我似乎是在「字典」格式文件...讀取和處理文本文件並保存到csv
文件頭如下:time,open,high,low,close,volume
下一行如下: 「t」:[1494257340],「o」:[206.7],「h」:[209.3],「l」:[204.50002],「c」:[204.90001],「v」:[49700650]} `
import csv
with open ('test_data.txt', 'rb') as f:
for line in f:
dict_file = eval(f.read())
time = (dict_file['t']) # print (time) result [1494257340]
open_price = (dict_file['o']) # print (open_price) result [206.7]
high = (dict_file['h']) # print (high) result [209.3]
low = (dict_file['l']) # print (low) result [204.50002]
close = (dict_file['c']) # print (close) result [204.90001]
volume = (dict_file['v']) # print (volume) result [49700650]
print (time, open_price, high, low, close, value)
# print result [1494257340] [206.7] [209.3] [204.50002] [204.90001] [49700650]
# I need to remove the [] from the output.
# expected result
# 1494257340, 206.7, 209.3, 204.50002, 204.90001, 49700650
結果我需要的是(變化時間( 「曆元日期格式」),以DD,毫米,YY
5/8/17, 206.7, 209.3, 204.50002, 204.90001, 49700650
,所以我知道我需要的csv.writer功能
你有沒有嘗試過的字符串轉換爲一個字節? – Afflicted
我對Python有點新,我不知道如何去觀看很多youtube視頻提示如何有時間如果你有時間 – bobbin
我的意思是我不能真正幫助你,因爲你只給了我們一部分你的代碼。甚至沒有你需要幫助的寫部分。但我至少可以爲你做到這一點。 https://docs.python.org/3.3/howto/unicode.html有很多方法可以將str轉換爲字節。你可以通過這個鏈接找到它們,它非常簡單。另外我想指出的是,您應該將close =(dict__file ['c'])重命名爲其他名稱,因爲它會與f.close()衝突。任何方式去那個鏈接它應該幫助你。你可以做value =(dict_file [b'v']),但它可能不起作用。 – Afflicted