繁体   English   中英

Python错误:“ TypeError:字符串索引必须为整数”

[英]Python Error:“TypeError: string indices must be integers”

我在这里尝试解析来自sql表的JSON字符串,但在解析时会出现错误TypeError: string indices must be integersresult = json_normalize(json_st,'results')处的TypeError: string indices must be integers

这是代码片段

from pandas.io.json import json_normalize
import pyodbc
cnxn = pyodbc.connect('connection string')
cursor = cnxn.cursor()

cursor.execute("select TOP 1 GEOCODE_ID, JSON from GEOCODE_TBL where GEOCODE_ID = 20")
ID=[]
JSON=[]

for row in cursor.fetchall():
      ID.append(row[0])
      JSON.append(row[1])


json_st1 = json.dumps(JSON)
json_st=json.loads(json_st1)

result = json_normalize(json_st,'results')

关于此的任何建议将有所帮助。

谢谢

多姆尼克。

查看函数签名,它需要一个数据变量和一些可选参数。

pandas.io.json.json_normalize(data, record_path=None, meta=None, 
meta_prefix=None, record_prefix=None, errors='raise', sep='.')

data : dict or list of dicts 因此,您不能将其作为字符串传递。 另外,为什么还要传递字符串'results'

您的数据变量必须是字典或字典列表(即python中的json)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM