用于Python的JSON转换为CSV

问题描述:

我知道这有很多问题,但我在操作数据时遇到了问题。用于Python的JSON转换为CSV

我有这样的名单:

['2017-05-31,20:00:00,71.1,73,', '2017-05-31,20:05:00,71.1,72.7,', '2017-05-31,20:10:00,71.1,72.5,', '2017-05-31,20:15:00,71.1,72.4,'] 

我需要这个JSON格式转换为CSV在CSV看起来像这样。

enter image description here

+1

如果您在使用代码时遇到问题,为什么不写一个包含该代码的问题,并要求修复它?而不是写一个像“请为我做我的工作”的问题?提示:阅读[mcve]和https://meta.*.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question – GhostCat

+1

那么,你有什么尝试?你有什么“麻烦”? –

+0

它看起来像一个列表而不是JSON。还有什么麻烦?你使用了'csv'模块吗? –

我建议:

my_list = ['2017-05-31,20:00:00,71.1,73,', '2017-05-31,20:05:00,71.1,72.7,', '2017-05-31,20:10:00,71.1,72.5,', '2017-05-31,20:15:00,71.1,72.4,'] 
# create and open a file for writing 
with open("my_file.csv", "w") as fout: 
    # iterate through your list 
    for element in my_list: 
     # write your element in your file plus a \n to trigger a new line 
     fout.write(element+"\n") 

等瞧!

import pandas as pd 
result = [] 
for item in myList: 
    row = item.split(',') 
    result.append(row) 
df = pd.DataFrame(result) 
df.to_csv("myFile.csv", index = False) 
+0

这对你有帮助吗? –