Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将要搜索的文件和字符串列表作为命令行参数传递_Python_Python 3.x - Fatal编程技术网

如何在python中将要搜索的文件和字符串列表作为命令行参数传递

如何在python中将要搜索的文件和字符串列表作为命令行参数传递,python,python-3.x,Python,Python 3.x,之前,我打开一个文件,使用函数搜索字符串列表 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

之前,我打开一个文件,使用函数搜索字符串列表

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

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

['[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="

我得到了问题的答案,我是这样做的:


    
import sys


def search_multiple_strings_in_file(file_name, list_of_strings):
    #local variabes declaration
    line_number = 0
    parsing = False
    list_of_results = []

    #opening the file as read_obj in read mode
    with open(file_name, 'r') as read_obj:
        for line in read_obj:
            line_number += 1
            for string_to_search in list_of_strings: #searching the string in the file
                if string_to_search in line: #if string is present in the particular line it will enter the if condition
                    print("================================================================================================")
                    print(line.strip())
                    next(read_obj)
                    for line1 in read_obj:       #In this for loop, printing the logs present between the two strings ">>>>>>backtrace_" and "<<<<<<backtrace_end"
                        if line1.startswith(">>>>>>backtrace_"):
                            parsing = True
                        if line1.strip().endswith("<<<<<<backtrace_end"):
                            parsing = False
                        if parsing == True:
                            print(line1.strip())
                        else:
                            print("<<<<<<backtrace_end\n")
                            break

        print("================================================================================================")


file_to_parse = sys.argv[1] #Assigning the argument to a variable
search_multiple_strings_in_file(file_to_parse, ['[ERROR]feature=Exception, action=signalhandler, no='])


对于作为参数的字符串列表,我们可以这样做:

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

    
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="


到底是什么问题?它没有得到期望的输出不是一个明确的问题陈述。你有错误吗?输出错误?您的编辑并没有真正改变任何东西。。。我们理解您无法获得所需的输出(如上所述)。你得到了什么输出?另外,由于问题是关于命令行参数的,请向我们展示您是如何运行脚本的,所以您基本上用另一个答案中的建议解决了问题,并就此写了一个答案?这不是这个网站的工作方式,对于那些花时间为你写答案的人来说是不尊重的。如果这对你有帮助,你可以投票和/或接受答案。不要使用其他答案的方法发布答案…@Tomerikoo,在他发布答案之前,我已经解决了答案,但我最近更新了这里,仅此而已,即使我之前对他的答案投了更高的票,我也发布了我的答案。不要太私人化。

    
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="