use*_*440 6 python csv json xlsx
像下面这样的 json,它是最外层的对象数组,还有进一步嵌套的对象数组。
data = [{"a": [{"a1": [{"id0": [{"aa": [{"aaa": 97}, {"aab": "one"}], "ab": [{"aba": 97}, {"abb": ["one", "two"]}]}]}, {"id1": [{"aa": [{"aaa": 23}]}]}]}, {"a2": []}]}, {"b": [{"b1": [{"Common": [{"bb": [{"value": 4}]}]}]}]}]
Run Code Online (Sandbox Code Playgroud)
我需要将其写入 csv(或 .xlsx 文件)
到目前为止我尝试过什么?
data_file = open('data_file.csv', 'w')
csv_writer = csv.writer(data_file)
for row in data:
csv_writer.writerow(row)
data_file.close()
Run Code Online (Sandbox Code Playgroud)
这给出了一个空文件“data_file.csv”。
另外我如何将标题添加到 CSV。我将标题存储在列表中,如下所示
hdrs = ['Section', 'Subsection', 'pId', 'Group', 'Parameter', 'Value']
Run Code Online (Sandbox Code Playgroud)
- 这对应于五个级别的键
预期的 CSV 输出
+---------+------------+--------+-------+-----------+----------+
| Section | Subsection | pId | Group | Parameter | Value |
+---------+------------+--------+-------+-----------+----------+
| a | a1 | id0 | aa | aaa | 97 |
| a | a1 | id0 | aa | aab | one |
| a | a1 | id0 | ab | aba | 97 |
| a | a1 | id0 | ab | abb | one, two |
| a | a1 | id1 | aa | aaa | 23 |
| a | a2 | | | | |
| b | b1 | Common | bb | value | 4 |
+---------+------------+--------+-------+-----------+----------+
Run Code Online (Sandbox Code Playgroud)
以下代码能够按照预期格式解析提供的数据。
from typing import List
def parse_recursive(dat)->List[List]:
ret=[]
if type(dat) is list:
for item in dat:
if type(item)==dict:
for k in item:
#print(k, item[k], sep=" # ")#debug print
if item[k]==[]: #empty list
ret.append([k])
else:
for l in parse_recursive(item[k]):
#print(k,l,sep=" : ") #debug print
ret.append([k]+l) #always returns List of List
else: #Right now only possibility is string eg. "one", "two"
return [[",".join(dat)]]
else: #can be int or string eg. 97, "23"
return [[dat]]
return ret
def write_to_csv(file_name:str, fields:List, row_data:List[List]):
import csv
with open(file_name, 'w') as csvfile:
# creating a csv writer object
csvwriter = csv.writer(csvfile)
# writing the fields
csvwriter.writerow(fields)
# writing the data rows
csvwriter.writerows(row_data)
if __name__=="__main__":
org_data = [{"a": [
{"a1": [
{"id0": [
{
"aa": [
{"aaa": 97},
{"aab": "one"}],
"ab": [
{"aba": 97},
{"abb": ["one", "two"]}
]
}
]
},
{"id1": [
{"aa": [
{"aaa": 23}]}]}
]
},
{"a2": []}
]},
{"b": [{"b1": [{"Common": [{"bb": [{"value": 4}]}]}]}]}]
print(parse_recursive(org_data)) #Debug
file_name="data_file.csv"
fields=['Section', 'Subsection', 'pId', 'Group', 'Parameter', 'Value']
write_to_csv(file_name, fields, parse_recursive(org_data))
Run Code Online (Sandbox Code Playgroud)
parse_recursive尝试根据我尝试从您的输入和输出格式推断的规则来解析任意深度字典。
以下是您提供的输入的输出parse_recursive-
mahorir@mahorir-Vostro-3446:~/Desktop$ python3 so.py
[['a', 'a1', 'id0', 'aa', 'aaa', 97], ['a', 'a1', 'id0', 'aa', 'aab', 'one'], ['a', 'a1', 'id0', 'ab', 'aba', 97], ['a', 'a1', 'id0', 'ab', 'abb', 'one,two'], ['a', 'a1', 'id1', 'aa', 'aaa', 23], ['a', 'a2'], ['b', 'b1', 'Common', 'bb', 'value', 4]]
Run Code Online (Sandbox Code Playgroud)
write_to_csv是一个写入 csv 文件的简单函数。
| 归档时间: |
|
| 查看次数: |
664 次 |
| 最近记录: |