简体   繁体   English

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

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

I'm here trying to parse JSON string that comes from a sql table, but while parsing it is giving the error TypeError: string indices must be integers at line result = json_normalize(json_st,'results') 我在这里尝试解析来自sql表的JSON字符串,但在解析时会出现错误TypeError: string indices must be integersresult = json_normalize(json_st,'results')处的TypeError: string indices must be integers

here is the code snippet 这是代码片段

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')

Any suggestion on this will be helpful. 关于此的任何建议将有所帮助。

Thanks 谢谢

Domnick. 多姆尼克。

Looking at the function signature, it takes one data variable and some optional params. 查看函数签名,它需要一个数据变量和一些可选参数。

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

Where data : dict or list of dicts . data : dict or list of dicts So you can't pass it as a string . 因此,您不能将其作为字符串传递。 Also, why are you passing the string 'results' too? 另外,为什么还要传递字符串'results'

Your data variable needs to be a dict, or list of dicts (which is, json in python) 您的数据变量必须是字典或字典列表(即python中的json)

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

相关问题 python 错误 - TypeError:字符串索引必须是整数 - python error - TypeError: string indices must be integers Python错误TypeError:字符串索引必须是整数 - Python error TypeError: string indices must be integers 如何修复 Python 中的“TypeError:字符串索引必须是整数”错误 - How to fix "TypeError: string indices must be integers" error in Python Python 和 JSON 错误 - TypeError:字符串索引必须是整数 - Python and JSON Error - TypeError: string indices must be integers python和json,错误-TypeError:字符串索引必须为整数 - python and json, Error - TypeError: string indices must be integers 类型错误:使用 Python 解析 Json 时,字符串索引必须是整数错误 - TypeError: string indices must be integers error when parsing Json with Python Python和网络X中的错误-TypeError:字符串索引必须是整数,而不是str - Error in Python and network X - TypeError: string indices must be integers, not str 如何在Python中修复'TypeError:字符串索引必须是整数'错误 - How to fix 'TypeError: string indices must be integers' error in Python Python Json TypeError:字符串索引必须是整数错误 - Python Json TypeError: string indices must be integers error Python:类型错误:字符串索引必须是 python 中的整数 - Python: TypeError: string indices must be integers in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM