我想按键读取数据,但下面的错误是有问题的
print(data[0]['user_id'])
TypeError: string indices must be integers
我的代码:
with open("AccInformation.json") as file:
data = json.loads(file.read())
print(data[0]['user_id'])
我的示例json文件:
[
"{"user_id": 88387467586, "firstName": "Murat", "lastName": "KO\u00c7", "email": "vehbikoc@kocholding.com.tr", "username": "kocholding", "password": "123456789", "accountKEY": "Y29K-G6LF-MRDI-PT5S-BZ1Q-VA4N", "register_date": "Mon Aug 30 02:01:31 2021"}",
"{"user_id": 33233303603, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "ZW2G-7960-HPQA-5SKX-LTDJ-YBCE", "register_date": "Mon Aug 30 14:42:39 2021"}",
"{"user_id": 86519529854, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "79DG-BNAZ-4IV3-OCKE-H5PM-UJ0T", "register_date": "Mon Aug 30 15:42:37 2021"}",
"{"user_id": 51814237688, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "SLVG-F2C6-0QOB-5UJ1-AIHZ-78X4", "register_date": "Mon Aug 30 22:46:48 2021"}"
]
我哪里做错了?
你这里有一个json字符串列表。你需要重新解析它们中的每一个,或者修复你的json。
with open("AccInformation.json") as file:
data = json.load(file)
print(json.loads(data[0])['user_id'])
或者更好:
with open() as file:
data = [json.loads(it) for it in json.load(file)]
print(data[0]['user_id'])
[]语法称为列表推导式。Alltough我建议你修复你的json代替
问题是您在AccInformation.json
文件中有一个字符串列表,并且您试图访问字符串的键user_id
,这是不可能的,因为字符串有整数作为索引。您首先需要做的是json.loads(data[0])
,它将字符串反序列化为python对象,然后您可以访问键user_id
。
应该是这样的:
with open("AccInformation.json") as file:
data = json.loads(file.read())
account = json.loads(data[0])
print(account["user_id"])
另一个提示:您可以将文件传递给json.load()
,因此您不需要调用file.read()
:
with open("AccInformation.json") as file:
data = json.load(file)
account = json.loads(data[0])
print(account["user_id"])
关于json
库和示例的更多信息:https://docs.python.org/3/library/json.html
你的json文件格式不对,能不能去掉"周围的地图,并删除转义的";在映射的键中,这样就不用:
[
"{"user_id": 88387467586, "firstName": "Murat", "lastName": "KO\u00c7", "email": "vehbikoc@kocholding.com.tr", "username": "kocholding", "password": "123456789", "accountKEY": "Y29K-G6LF-MRDI-PT5S-BZ1Q-VA4N", "register_date": "Mon Aug 30 02:01:31 2021"}",
"{"user_id": 33233303603, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "ZW2G-7960-HPQA-5SKX-LTDJ-YBCE", "register_date": "Mon Aug 30 14:42:39 2021"}",
"{"user_id": 86519529854, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "79DG-BNAZ-4IV3-OCKE-H5PM-UJ0T", "register_date": "Mon Aug 30 15:42:37 2021"}",
"{"user_id": 51814237688, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "SLVG-F2C6-0QOB-5UJ1-AIHZ-78X4", "register_date": "Mon Aug 30 22:46:48 2021"}"
]
应该
[
{"user_id": 88387467586, "firstName": "Murat", "lastName": "KOu00c7", "email": "vehbikoc@kocholding.com.tr", "username": "kocholding", "password": "123456789", "accountKEY": "Y29K-G6LF-MRDI-PT5S-BZ1Q-VA4N", "register_date": "Mon Aug 30 02:01:31 2021"},
{"user_id": 33233303603, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "ZW2G-7960-HPQA-5SKX-LTDJ-YBCE", "register_date": "Mon Aug 30 14:42:39 2021"},
{"user_id": 86519529854, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "79DG-BNAZ-4IV3-OCKE-H5PM-UJ0T", "register_date": "Mon Aug 30 15:42:37 2021"},
{"user_id": 51814237688, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "SLVG-F2C6-0QOB-5UJ1-AIHZ-78X4", "register_date": "Mon Aug 30 22:46:48 2021"}
]
然后它将正常工作,您可以访问您的数据如下:
data[0]["user_id" or indeed data[0].user_id
如果你必须坚持你的文件结构:(但请注意这个语法不是JSON文件)
[
"{"user_id": 88387467586, "firstName": "Murat", "lastName": "KO\u00c7", "email": "vehbikoc@kocholding.com.tr", "username": "kocholding", "password": "123456789", "accountKEY": "Y29K-G6LF-MRDI-PT5S-BZ1Q-VA4N", "register_date": "Mon Aug 30 02:01:31 2021"}",
"{"user_id": 33233303603, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "ZW2G-7960-HPQA-5SKX-LTDJ-YBCE", "register_date": "Mon Aug 30 14:42:39 2021"}",
"{"user_id": 86519529854, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "79DG-BNAZ-4IV3-OCKE-H5PM-UJ0T", "register_date": "Mon Aug 30 15:42:37 2021"}",
"{"user_id": 51814237688, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "SLVG-F2C6-0QOB-5UJ1-AIHZ-78X4", "register_date": "Mon Aug 30 22:46:48 2021"}"
]
使用regex,您可以这样做,以便在运行时将当前文件转换为适当的json文件:(按原样工作)
with open("AccInformation.json") as file:
cleaned = file.read().replace(r"(?<!\)", "")
cleaned = '{ n "data": ' + cleaned + 'n }'
pre_data = json.loads(cleaned)
data = [json.loads(x) for x in pre_data.get('data')]
print(data[0]['user_id'])
>>> 88387467586
否则,请重构你的代码,使你的json文件看起来像这样:
{
"data": [
{"user_id": 88387467586, "firstName": "Murat", "lastName": "KO\u00c7", "email": "vehbikoc@kocholding.com.tr", "username": "kocholding", "password": "123456789", "accountKEY": "Y29K-G6LF-MRDI-PT5S-BZ1Q-VA4N", "register_date": "Mon Aug 30 02:01:31 2021"},
{"user_id": 33233303603, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "ZW2G-7960-HPQA-5SKX-LTDJ-YBCE", "register_date": "Mon Aug 30 14:42:39 2021"},
{"user_id": 86519529854, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "79DG-BNAZ-4IV3-OCKE-H5PM-UJ0T", "register_date": "Mon Aug 30 15:42:37 2021"},
{"user_id": 51814237688, "firstName": "Sema", "lastName": "UZUN", "email": "sema12@gmail.com", "username": "semazen", "password": "123456789", "accountKEY": "SLVG-F2C6-0QOB-5UJ1-AIHZ-78X4", "register_date": "Mon Aug 30 22:46:48 2021"}
]
}
然后你可以按预期调用:
with open("AccInformation.json") as file:
data = json.loads(file.read())["data"]
print(data[0]['user_id'])