【发布时间】:2017-05-09 17:26:30
【问题描述】:
我的文件似乎是“dict”格式...
文件头如下: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,mm,yy
5/8/17, 206.7, 209.3, 204.50002, 204.90001, 49700650
所以我知道我需要 csv.writer 函数
【问题讨论】:
-
你试过把字符串转换成字节吗?
-
我对python有点陌生,我不知道怎么做,看了很多youtube视频告诉我如果你有时间怎么做
-
我的意思是我不能真正帮助你,因为你只给了我们你的一部分代码。甚至不是您需要帮助的写入部分。但至少我可以为你做到这一点。 docs.python.org/3.3/howto/unicode.html 有很多方法可以将 str 转换为字节。您可以通过该链接找到它们,而且非常简单。另外我想指出,您应该将 close = (dict__file['c']) 重命名为其他名称,因为它会与 f.close() 冲突。任何方式去那个链接它应该帮助你。你可以做 value = (dict_file[b'v']) 但它可能不起作用。
-
谢谢,我去看看链接
-
如果你“看了很多python教程”,你应该知道
[...]在python中是什么意思
标签: python string python-3.x csv byte