简体   繁体   中英

Python How do you loop through a list of strings that contain variables?

I am working with some data and building a list of queries based upon what incident_source . This was working fine when I didn't have variable added to it which requires the + symbols. When I added that I am getting: TypeError: can only concatenate str (not "list") to str . Does anyone know a way around this or a better solution?

source = 1
offense_id = [122,153,142]
incident_source = ['source_1', 'source_2']
num_searches_by_id = []
queries_needed_ran = []

for i,j in zip(incident_source, offense_id):
    if i == 'source_1':
        data = ['''SELECT QIDNAME(qid) AS 'Event Name',"Request Method" FROM events WHERE URL=REPLACEFIRST('hxxp',''' + "'" + source + "'" + ''','http') START ''' + str(start_time) + '''-43200000''',
                '''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(source) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']
        num_searches_by_id.append(len(data))
        for x in data:
            queries_needed_ran.append(x)
            
    elif i == 'source_2':
        data = ['''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(source) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']
        num_searches_by_id.append(len(data))
        for x in data:
            queries_needed_ran.append(x)
    else:
        num_searches_by_id.append(0)


runfile('/Users/thomas.gorman/Documents/Python Connectors/riskiq_passivetotal/untitled20.py', wdir='/Users/thomas.gorman/Documents/Python Connectors/riskiq_passivetotal')
Traceback (most recent call last):

  File "/Users/me/Documents/Python Connectors/untitled20.py", line 427, in <module>
    data = ['''SELECT QIDNAME(qid) AS 'Event Name',"Request Method" FROM events WHERE URL=REPLACEFIRST('hxxp',''' + "'" + source + "'" + ''','http') START ''' + str(start_time) + '''-43200000''',

TypeError: can only concatenate str (not "list") to str`

Expected output:

num_searches_by_id = ['''SELECT QIDNAME(qid) AS 'Event Name',"Request Method" FROM events WHERE URL=REPLACEFIRST('hxxp',''' + "'" + source + "'" + ''','http') START ''' + str(start_time) + '''-43200000''',
                '''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(source) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''',
'''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(source) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']

So I'm not sure about some of your variables and where they are coming from, but I got rid of the TypeError: can only concatenate str (not "list") to str by placing brackets for the index your looking for in your if statement and changing the variables. Also not sure where start_time is coming from...

offense_id = [122,153,142]
incident_source = ['source_1', 'source_2']
num_searches_by_id = []
queries_needed_ran = []

for i,j in zip(incident_source, offense_id):
    if i == 'source_1':
        data = ['''SELECT QIDNAME(qid) AS 'Event Name',"Request Method" FROM events WHERE URL=REPLACEFIRST('hxxp',''' + "'" + incident_source[0] + "'" + ''','http') START ''' + str(start_time) + '''-43200000''',
                '''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(incident_source[0]) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']
    num_searches_by_id.append(len(data))
    for x in data:
        queries_needed_ran.append(x)
        
    elif i == 'source_2':
        data = ['''select QIDNAME(qid) as 'Event Name', "Recepient" from events where ( "URL"=''' + "'" + str(incident_source[1]) + "'" + '''AND logSourceId='1' ) START ''' + str(start_time) + '''-43200000''']
        num_searches_by_id.append(len(data))
        for x in data:
            queries_needed_ran.append(x)
    else:
        num_searches_by_id.append(0)

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