简体   繁体   中英

Python - string indices must be integers

I apologize if this is a basic fix and the length of the post, but I am new to Python. I've also included a large chunk of the script for context purposes.

I am using a script to pull scanning data from JSON into a MySQL DB. The script was working fine until an update was released.

Now when I run the script I receive the error:

for result in resultc['response']['results']: TypeError: string indices must be integers

Before this update I knew the data types for each value, but this has changed and I cannot pinpoint where. Is there a way to convert each value to be recognized as a string?

# 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"]))

Put this before the for loop to work out which object is the string (I guess it's probably the second one)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM