【发布时间】:2013-02-14 22:17:33
【问题描述】:
如果这是基本修复和帖子的长度,我深表歉意,但我是 Python 新手。出于上下文目的,我还包含了大部分脚本。
我正在使用脚本将扫描数据从 JSON 提取到 MySQL 数据库中。在发布更新之前,该脚本运行良好。
现在当我运行脚本时收到错误:
对于 resultc['response']['results'] 的结果: TypeError: 字符串索引必须是整数
在此更新之前,我知道每个值的数据类型,但这已经改变,我无法确定位置。有没有办法将每个值转换为字符串?
# 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"]))
【问题讨论】:
-
错误告诉您
resultc['response']是String,并且您不能引用它的results索引(因为字符串在技术上是字符元组)。请发布print(resultc)的输出 -
我会在早上发布输出,谢谢。
-
尝试找到你的列表,然后找到一个整数来索引它。
-
响应 "{'status': '200', 'content-length': '239', 'expires': 'Thu, 19 Nov 1981 08:52:00 GMT', 'server' : 'Apache', 'pragma': 'no-store', 'cache-control': 'no-store, no-cache, must-revalidate, post-check=0, pre-check=0', 'date' : 'Fri, 15 Feb 2013 15:11:41 GMT', 'content-type': 'application/json'}" Conetnt "{"type":"regular","request_id":"1","response" :"","error_code":146,"error_msg":"为 vuln 查询指定的参数无效。\n过滤器 'lastSeen' 必须采用以下格式:
: 。\n","warnings ":[],"timestamp":1360941101}" -
之前有不少数据,但是发帖太多了