我使用poloniex trader api获取实时市场自动收报机信息。它在控制台上运行完美。当我在回报中请求市场代码时,我得到{'last': '0.07269671', 'high24hr': '0.07379970', 'quoteVolume': '71582.56540639', 'isFrozen': '0', 'lowestAsk': '0.07277290', 'percentChange': '-0.00551274', 'id': 148, 'low24hr': '0.07124645', 'highestBid': '0.07269671', 'baseVolume': '5172.41552885'}
问题是它只存储项目名称/列表名称,例如 - low24hr, lowestAsk, highestBid etc
。不像他们的价值 - low24hr : 0.07124645
polo = Poloniex()
ticker_data = (polo('returnTicker')['BTC_ETH'])
out = csv.writer(open("myfile.csv","w"), delimiter=',',quoting=csv.QUOTE_ALL,)
out.writerow(ticker_data)
print(ticker_data)
以下是我的csv文件的样子 -
答案 0 :(得分:1)
with open('my_file.csv', 'w') as f:
for key, value in ticker_data.items(): f.write('{0},{1}\n'.format(key, value))
来自here。
答案 1 :(得分:1)
您的问题是out.writerow(ticker_data)
只接受字典的键并将它们写入文件。尝试使用csv.DictWriter
:
with open('myfile.csv', 'w', newline='') as csv_file:
# Pass the keys of the `ticker_data` as the fieldnames.
writer = csv.DictWriter(csv_file, fieldnames=ticker_data, quoting=csv.QUOTE_ALL)
# Write the fieldnames.
writer.writeheader()
# Write the values.
writer.writerow(ticker_data)