更快地解析文件中的所有列表元素并根据列表元素追加到新文件中
我正在尝试解析每个类似的带有 threadid 的日志文件。可以配置任意数量的线程。所有线程都写入同一个日志文件,我正在解析日志文件并创建特定于每个线程的新文件,以便稍后检查它们。
下面我在列表中捕获 threadid。
下面的代码正在完成这项工作,但我觉得这效率不高。还能有什么更快的吗?
sThdiD = ["abc", "cde\"efg"]
folderpath = "newdir"
os.system("mkdir " + folderpath)
for line in open(filetoopen):
for i in sThdiD:
if i in line:
open(folderpath+"/"+i+".log","a+").write(line)
I am trying to parse a log file with threadids in every like. There could be any number of threads that can be configured. All threads write to the same log file and I am parsing the log file and creating new files specific for each thread in order to check them later.
Below I am capturing the threadids in a list.
The below code is doing the job but I feel this is not efficient. Can there be anything faster ?.
sThdiD = ["abc", "cde\"efg"]
folderpath = "newdir"
os.system("mkdir " + folderpath)
for line in open(filetoopen):
for i in sThdiD:
if i in line:
open(folderpath+"/"+i+".log","a+").write(line)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您可以将整个日志文件放入内存中,我会保留一个字典,将线程 ID 映射到该线程写入的行,然后在最后写出整个文件。
如果您无法将整个日志文件保留在内存中,请尝试多次写入解决方案,一次写入一个文件。
Assuming you can fit the whole log file into memory, I'd keep a dictionary mapping thread IDs to lines written by that thread, and then write out whole files at the end.
If you can't keep the whole log file in memory, try a multipass solution in which you write each file one at a time.