类型错误:read_excel() 在使用熊猫将单个 excel 行导出到 JSON 文件时出现意外的关键字参数"index"



例如,我有一个10行电子表格,并希望将每一行导出为单个JSON文件名行号。Json(也就是第0行)将是"0. Json "然而,我得到这个错误,我不确定我可能做错了什么。代码如下:

import pandas as pd
from pathlib import Path
from pandas import read_excel
df = pd.read_excel("/fullpath/excel.xlsx", index=[0, 10], columns=['A']) 
for index, row in df.iterrows():
with open((str([index]) + ".json"), "w") as f:
f.write(row.to_string(row))

如果我不包括索引,然后我获得超过10行所有的列表包含信息从行0。

编辑:

我已经调整了代码,并尝试如下:

import pandas as pd
from pathlib import Path
from pandas import read_excel
df = pd.read_excel("/fullpath/excel.xlsx") 
for index, row in df.iterrows():
row.to_json(str(index)+'.json')

这给了我多个json文件,都有第一行的信息。它似乎越来越近了。知道每个文件如何反映下一行吗?

观察,在代码df = pd.read_excel("/fullpath/excel.xlsx", index=[0, 10], columns=['A'])中索引和列均无显著性。

如果你只想让每一行都是json格式,你可以使用下面的命令,它会在脚本所在的文件夹中创建json文件。

for index, row in df.iterrows():
#got the row and then write the row
row.to_json(str(index)+'.json')# method from https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.to_json.html