如何使用以下变量数据创建json对象/ csv:
out = [['core java'],['angular js']]
skills = 'Java'
有人可以告诉我如何获得预期输出中所示的json对象/ csv吗?
预期输出:
Java
0 core java
1 angular js
答案 0 :(得分:1)
尝试以下示例:
import json
data = {}
data['dynamic_col_name'] = 'dynamic_upcoming_value'
json_data = json.dumps(data)
答案 1 :(得分:0)
使用csv module:
使用list
:
import csv
out = [['Java'], ['core java'],['angular js']]
with open('some.csv', 'w', newline='') as f:
writer = csv.writer(f)
writer.writerows(out)
使用dict
:
import csv
out = [['core javaaaa'],['angular js']]
skills = ['Java']
with open('names.csv', 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=skills)
writer.writeheader()
rows = [{skills[idx]: cell for idx, cell in enumerate(row)} for row in out]
writer.writerows(rows)