2017-07-03 82 views
0

我知道這有很多問題,但我在操作數據時遇到了問題。用於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.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question – GhostCat

+1

那麼,你有什麼嘗試?你有什麼「麻煩」? –

+0

它看起來像一個列表而不是JSON。還有什麼麻煩?你使用了'csv'模塊嗎? –

回答

2

我建議:

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") 

等瞧!

1
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

這對你有幫助嗎? –