Python如何循环遍历包含变量的字符串列表?

2024-06-16 09:36:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在处理一些数据,并根据incident_source构建一个查询列表。当我没有添加需要+符号的变量时,它工作得很好。当我添加时,我得到:TypeError: can only concatenate str (not "list") to str。有人知道解决这个问题的方法或更好的解决方案吗

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`

预期产出:

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''']

Tags: nameeventidurlsourcedatatimeevents
1条回答
网友
1楼 · 发布于 2024-06-16 09:36:09

因此,我不确定您的一些变量以及它们来自何处,但我通过在if语句中为您要查找的索引放置括号并更改变量,从而消除了TypeError: can only concatenate str (not "list") to str。也不确定start_time来自哪里

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)

相关问题 更多 >