小编God*_*yer的帖子

尝试在 CSV 文件中写入行时,Python“列表”对象没有属性“键”

我正在尝试将新行写入 CSV 文件,但不能,因为我在 Python Shell 中收到错误。

下面是我正在使用的代码(我正在从 API 读取 JSON 并希望将数据放入 CSV 文件中)

# import urllib library
from urllib.request import Request, urlopen
c=1
# import json
import json
# store the URL in url as 
# parameter for urlopen
import pandas as pd
import csv
headerList = ['name','id','order','height','weight','speed','special_defense','special_attack','defense','attack','hp']
  
# open CSV file and assign header
with open("pokemon_stats.csv", 'w') as file:
    dw = csv.DictWriter(file, delimiter=',', 
                        fieldnames=headerList)
    dw.writeheader()
  
# display csv file
fileContent = pd.read_csv("pokemon_stats.csv")
for r in range(1,3):
    req = Request('https://pokeapi.co/api/v2/pokemon/'+str(r)+'/', …
Run Code Online (Sandbox Code Playgroud)

python csv json pandas

2
推荐指数
1
解决办法
2555
查看次数

标签 统计

csv ×1

json ×1

pandas ×1

python ×1