簡體   English   中英

使用python3將json數據轉換為csv但它不起作用

[英]Convert json data to csv with python3 but it doesn't work

我有一個包含一組數字的大數據集

數據是從服務器獲取的

每行數據是這樣的:

{"body_data":[
{'height': 170.00, 'weight': 165.00, 'blood': 3.00, 'sugar': 100.02, 'fat': 36.02, 'time_object': 1544443260000},
{'height': 170.00, 'weight': 165.00, 'blood': 3.00, 'sugar': 100.02, 'fat': 36.02, 'time_object': 1544443260000},
],"symbol":"DATA_FAT","empty":false}

我試圖將數據保存為 *.json 格式,將其作為新文件導入並用 csv 重寫,但出現錯誤。

我用熊貓嘗試了以下代碼:

df = pd.DataFrame.from_dict(data, orient='index',columns=['open', 'height', 'weight', 'blood', 'sugar', 'fat', 'time_object'])

它給了我以下錯誤:

 File "pandas/_libs/src/inference.pyx", line 1517, in pandas._libs.lib.to_object_array
TypeError: object of type 'bool' has no len()

誰能幫幫我

我相信您需要選擇嵌套鍵body_data

df = pd.DataFrame(data['body_data'])
print (df)
   blood    fat  height   sugar    time_object  weight
0    3.0  36.02   170.0  100.02  1544443260000   165.0
1    3.0  36.02   170.0  100.02  1544443260000   165.0

如果想要更改列的順序( open鍵不在示例數據中,因此輸出中為NaN ):

df = pd.DataFrame(data['body_data'],
                  columns=['open', 'height', 'weight', 'blood', 'sugar', 'fat', 'time_object'])
print (df)
   open  height  weight  blood   sugar    fat    time_object
0   NaN   170.0   165.0    3.0  100.02  36.02  1544443260000
1   NaN   170.0   165.0    3.0  100.02  36.02  1544443260000

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM