问题描述
目前正在编写一些代码来组织我从 API 收到的数据。
它带有键和值,但我还没有找到一种方法来制作一个整洁的 CSV,其中包含键的列和值的行。
当数据通过 API 传入时,它会存储在一个 txt 文件中。
我将那个 txt 文件通过用 \n 替换行尾的字符来对其进行排序。
# Read in the file
with open('latest_out.txt','r') as file :
filedata = file.read()
# Replace the target string
filedata = filedata.replace('}}},','\n')
# Write the file out again
with open('latest_out_sorted.txt','w') as file:
file.write(filedata)
这给了我一个包含每个加密货币的部分。
但我需要将其写入包含列和值的 CSV。
文本示例:
{'id': 1,'name': 'Bitcoin','symbol': 'BTC','slug': 'bitcoin','num_market_pairs': 9716,'date_added': '2013-04-28T00:00:00.000Z','tags': ['mineable','pow','sha-256','store-of-value','state-channels','coinbase-ventures-portfolio','three-arrows-capital-portfolio','polychain-capital-portfolio'],'max_supply': 21000000,'circulating_supply': 18636068,'total_supply': 18636068,'platform': None,'cmc_rank': 1,'last_updated': '2021-02-23T02:28:02.000Z','quote': {'CAD': {'price': 65576.2472947517,'volume_24h': 112161256452.41504,'percent_change_1h': -2.63752472,'percent_change_24h': -8.8113568,'percent_change_7d': 6.37461057,'percent_change_30d': 62.70380758,'market_cap': 1222083403769.8086,'last_updated': '2021-02-23T02:28:05.000Z'
{'id': 1027,'name': 'Ethereum','symbol': 'ETH','slug': 'ethereum','num_market_pairs': 5997,'date_added': '2015-08-07T00:00:00.000Z','smart-contracts','max_supply': None,'circulating_supply': 114783223.6865,'total_supply': 114783223.6865,'cmc_rank': 2,'quote': {'CAD': {'price': 2147.126992405774,'volume_24h': 52728131634.694145,'percent_change_1h': -3.49137429,'percent_change_24h': -11.16189412,'percent_change_7d': -6.56833787,'percent_change_30d': 37.20883417,'market_cap': 246454157852.63397,'last_updated': '2021-02-23T02:28:05.000Z'
希望这有助于理解我的目标是什么。希望能够将CSV中的数据直接读取到网页中。
谢谢,
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)