2017-03-07 50 views
1

我正在嘗試寫入文本文件以記錄正在處理的數據。我在一個python文件中做了這個,它工作的很好,但是當我從一個Jupyter筆記本嘗試時失敗了。無法從Jupyter筆記本登錄到文本文件

什麼從Python文件,我試圖在筆記本使用的工作是這樣的:

f = open('./data/data_log/log'+ str(time) +'.txt', 'w') 
print >> f, '#########################################' 
print >> f, 'New log opened' 
print >> f, '#########################################' 

然後,在該文件中,當某個變量的推移,我希望寫的,所以我可以跟蹤數據正在流經管道。在Python的文件我實現了同樣的方式,例如:

# Printing this data to the file. 
print >> f, 'Steering check :', steering_check 

的「>>」好像是不支持的,因爲它拋出一個錯誤提的是。

jupyter notebook unsupported operand type(s) for >>: 

我無法通過谷歌搜索和搜索這裏找到一種方法來做到這一點。

任何人都可以在這裏指出我正確的方向,甚至鏈接到文檔或什麼東西就足夠了,我只是無法找到答案顯示如何在Jupyter筆記本中做到這一點。

回答

1

您應該使用:f.write('#######')

Jupyter筆記本中不支持print chevron語法。

查看此question中兩種語法的區別。

您必須自行在參數和行結束符之間放置空格。

1

你可以使用.write()其中IMO看起來更好

f = open('./data/data_log/log'+ str(time) +'.txt', 'w') 
f.write('#########################################\n') 
f.write('New log opened\n') 
f.write('#########################################\n') 

而且

f.write('Steering check : ' + steering_check + "\n")