[英]Python Error:“TypeError: string indices must be integers”
我在這里嘗試解析來自sql表的JSON字符串,但在解析時會出現錯誤TypeError: string indices must be integers
行result = 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.