Python - 字符串索引必须是整数
抱歉如果这个问题很基础,而且帖子有点长,但我刚开始学Python。为了让大家更好理解,我还附上了大段的代码。
我在用一个脚本把扫描数据从JSON格式导入到MySQL数据库。这个脚本之前运行得很好,但最近更新后就出问题了。
现在当我运行这个脚本时,出现了这个错误:
for result in resultc['response']['results']: TypeError: string indices must be integers
在更新之前,我知道每个值的数据类型是什么,但现在这些类型变了,我找不到具体的原因。有没有办法把每个值转换成字符串格式呢?
# Send the cumulative JSON and then populate the table
cumresponse, content = SendRequest(url, headers, cumdata)
resultc = json.loads(content)
off = 0
print "\nFilling cumvulndata table with vulnerabilities from the cumulative database. Please wait..."
for result in resultc['response']['results']:
off += 1
print off, result
cursor.execute ("""INSERT INTO cumvulndata(
offset,pluginName,repositoryID,
severity,pluginID,hasBeenMitigated,
dnsName,macAddress,familyID,recastRisk,
firstSeen,ip,acceptRisk,lastSeen,netbiosName,
port,pluginText,protocol) VALUES
(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,(FROM_UNIXTIME(%s)),%s,%s,(FROM_UNIXTIME(%s)),%s,%s, %s,%s)""" , (off,result["pluginName"],result["repositoryID"]),result["severity"]),
result["pluginID"]), result["hasBeenMitigated"]),result["dnsName"],
result["macAddress"],result["familyID"]),result["recastRisk"]),
result["firstSeen"],result["ip"],result["acceptRisk"],result["lastSeen"],
result["netbiosName"],result["port"],result["pluginText"],result["protocol"]))
1 个回答
1
在这个 for 循环之前放上这段代码,以找出哪个对象是字符串(我猜可能是第二个对象)。
print type(resultc)
print type(resultc['response'])