python - 如何将文件和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])
但它没有得到正确的输出(正确的输出是我上面写的那个)。
解决方案
['[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="
推荐阅读
- sql - 如何使用 RedShift 合并 2 个结果集
- google-analytics - 尽管使用了 UserID,但在 App+Web 分析中针对活跃/总用户计算了多个设备 ID
- linux - 2 个 AWS EC2 实例之间的 rsync:找不到 .pem 文件
- javascript - 将外部变量传递给弹出窗口中的字段:(arcgis-js-api)
- apache-spark - 从 pyspark 读取 hbase
- java - 无法处理部件,因为没有提供多部件配置 enctype='multipart/form-data' 使用问题
- c++ - 编译在我的 Mac 上工作,但不在集群上(Linux)
- node.js - 如何将文件上传到 mongodb GridFs?
- python - Python for 循环乘法列表
- java - 在 Service 类的构造函数中使用 JPA 存储库访问