首页 > 解决方案 > 如何将文件和list_of_strings作为python中的命令行参数进行搜索

问题描述

以前我正在打开一个文件以使用函数搜索字符串列表

def search_multiple_strings_in_file(file_name, list_of_strings):
    line_number = 0
    parsing = False
    list_of_results = []
    with open(file_name, 'r') as read_obj:
        for line in read_obj:
            line_number += 1
            for string_to_search in list_of_strings:
                if string_to_search in line:
                    print("================================================================================================")
                    print('Line:' , line_number, '', line.strip())
                    #print('[ERROR]feature = ',Exception, ", action = ", 'signalhandler' , ", no = ", 'signalno')
                    #print("-----------------------------------------------------------------------------------------------")
                    next(read_obj)
                    for line1 in read_obj:
                        if line1.startswith(">>>>>>backtrace_"):
                            parsing = True
                        if line1.strip().endswith("<<<<<<backtrace_end"):
                            parsing = False
                        if parsing == True:
                            print(line1.strip())
                        else:
                            print("<<<<<<backtrace_end")
                            break

search_multiple_strings_in_file('CrashDump.log', ['[ERROR]feature=Exception, action=signalhandler, no='])

上述代码的输出:

-----------------------------------------------------------------------------------------------
Line: 980  [ERROR]feature=Exception, action=signalhandler, no=14[SIGALRM] Thread[TKEL_Timer]
-----------------------------------------------------------------------------------------------
>>>>>>backtrace_start
0x75fc7edc : /usr/lib/libchal.so.1(+0x29edc) [0x75fc7edc]
0x770c08e8 : linux-vdso.so.1(+0x8e8) [0x770c08e8]
0x76c12854 : /lib/libpthread.so.0(+0x6854) [0x76c12854]
<<<<<<backtrace_end
-----------------------------------------------------------------------------------------------
Line: 1114  [ERROR]feature=Exception, action=signalhandler, no=1[SIGHUP] Thread[TASK_PLAYER]
-----------------------------------------------------------------------------------------------
>>>>>>backtrace_start
0x75fc7edc : /usr/lib/libchal.so.1(+0x29edc) [0x75fc7edc]
0x770c08e8 : linux-vdso.so.1(+0x8e8) [0x770c08e8]
0x76c2031a : /lib/libpthread.so.0(raise+0x3a) [0x76c2031a]
<<<<<<backtrace_end

但是,现在我需要传递文件名并list_of_strings作为命令行参数。我试过这样:

if __name__ == '__main__':
    if len(sys.argv) == 2:
        print("Pass a file to parse as an argument")
        sys.exit()
    else:
        search_multiple_strings_in_file(sys.argv[1], sys.argv[2])

但它没有得到正确的输出(正确的输出是我上面写的那个)。

标签: pythonpython-3.x

解决方案


['[ERROR]feature=Exception, action=signalhandler, no=']是一个包含字符串的数组。sys.argv[2]是一个字符串。

你需要

if __name__ == '__main__':
    if len(sys.argv) == 2:
        print("Pass a file to parse as an argument")
        sys.exit()
    else:
        search_multiple_strings_in_file(sys.argv[1], sys.argv[2:])

现在第一个命令行参数是文件名,下面的参数作为函数的第二个参数在列表中传递,例如

python3 prog.py "CrashDump.log" "[ERROR]feature=Exception, action=signalhandler, no="

推荐阅读