我有以下JSON格式,我想将其转换为CSV文件。 这个数据基本上是股票仓位。我想下载 每5分钟位置......如果需要,关闭位置。我下载了json数据但无法转换为csv
'net':[
{
'tradingsymbol':'IDBI',
'exchange':'NSE',
'instrument_token':377857,
'product':'BO',
'quantity':0,
'overnight_quantity':0,
'multiplier':1,
'average_price':0,
'close_price':0,
'last_price':57.8,
'value':-15.75,
'pnl':-15.75,
'm2m':-15.75,
'unrealised':-15.75,
'realised':0,
'buy_quantity':35,
'buy_price':57.5,
'buy_value':2012.5,
'buy_m2m':2012.5,
'sell_quantity':35,
'sell_price':57.05,
'sell_value':1996.75,
'sell_m2m':1996.75,
'day_buy_quantity':35,
'day_buy_price':57.5,
'day_buy_value':2012.5,
'day_sell_quantity':35,
'day_sell_price':57.05,
'day_sell_value':1996.75
} ], 'day':[
{
'tradingsymbol':'IDBI',
'exchange':'NSE',
'instrument_token':377857,
'product':'BO',
'quantity':0,
'overnight_quantity':0,
'multiplier':1,
'average_price':0,
'close_price':0,
'last_price':57.8,
'value':-15.75,
'pnl':-15.75,
'm2m':-15.75,
'unrealised':-15.75,
'realised':0,
'buy_quantity':35,
'buy_price':57.5,
'buy_value':2012.5,
'buy_m2m':2012.5,
'sell_quantity':35,
'sell_price':57.05,
'sell_value':1996.75,
'sell_m2m':1996.75,
'day_buy_quantity':35,
'day_buy_price':57.5,
'day_buy_value':2012.5,
'day_sell_quantity':35,
'day_sell_price':57.05,
'day_sell_value':1996.75
} ] } emp_data = employee_parsed['data']
print(emp_data)
employ_data = open('C:/watch/Position.csv', 'w',newline='')
print(employ_data)
csvwriter = csv.writer(employ_data)
count = 0
for emp in emp_data:
if count == 0:
header = emp.keys()
csvwriter.writerow(header)
count += 1
csvwriter.writerow(emp.values())
employ_data.close()
在此行获取此错误
header = emp.keys()
属性错误:' str'对象没有属性'键'
请解决任何问题。
答案 0 :(得分:0)
看起来你有一个JSON字符串
尝试:
import json
emp_data = json.loads(employee_parsed['data'])