【问题标题】:Python - string indices must be integersPython - 字符串索引必须是整数
【发布时间】: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","re​​quest_id":"1","re​​sponse" :"","error_code":146,"error_msg":"为 vuln 查询指定的参数无效。\n过滤器 'lastSeen' 必须采用以下格式::。\n","warnings ":[],"timestamp":1360941101}"
  • 之前有不少数据,但是发帖太多了

标签: python json string


【解决方案1】:

把这个放在for循环之前,找出哪个对象是字符串(我猜它可能是第二个)

print type(resultc)
print type(resultc['response'])

【讨论】:

  • 我会在早上尝试并发布结果,我的服务器因维护而关闭。
猜你喜欢
  • 1970-01-01
  • 2016-03-26
  • 2016-02-17
  • 2023-03-03
  • 1970-01-01
  • 1970-01-01
  • 2022-11-02
  • 2019-04-26
  • 2018-10-23
相关资源
最近更新 更多