python - 加载json时出错?键必须是字符串
问题描述
尝试读取我的 json 数据,但在使用 pandas 时显示此错误
import pandas as pd
data=pd.read_json(r"1_lines.json")
print(data)
错误
ValueError: Key name of object must be 'string' when decoding 'object'
my_json
{1: [((86, 27, 169, 50), 'PSEG'), ((323, 33, 408, 41), 'Total amount due'), ((499, 26, 591, 44), '$26,930.17'), ((85, 53, 195, 63), 'We make things work for you.')],
2: [((427, 24, 560, 31), ' LLC'), ((422, 35, 560, 42), 'Your account number: 4246'), ((454, 44, 560, 50), 'Invoice Number: 60438'), ((53, 82, 170, 93), 't Metering Program')]}
使用其他代码时
import json
json_data= open("1_lines.json").read()
json_obj=json.loads(json_data)
print(json_obj)
错误
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)```
解决方案
你的 my_json 是字典而不是 json。T0 从字典转换json
import json
a=json.dumps(my_json)
推荐阅读
- excel - 如何检查列表框的一行是否为空
- javascript - 将我的代码转换为使用 ie11 并收到错误
- struct - 可区分联合的结构属性
- linux - 复制文件而不删除目标功能
- python - 在特定深度的 dict 上添加项目
- julia - 如何在两个for循环中使用break?
- sql-server - 如何获取每组中的最后一行
- c - c 的 if、switch 和 while 条件中的 print 语句
- python - *为什么*不是 run_until_complete 可重入。如何在没有线程的情况下逐步移植到异步?
- sql-server - 将具有不同列的多个表合并到一个表中