[英]Copy select lines from many text files and paste to new file
I'm new to Python and trying to use it to do what I think should be a very simple task. 我是Python的新手,尝试使用它来完成我认为应该非常简单的任务。 I have a folder with many .log files, which each have many lines of data.
我有一个包含许多.log文件的文件夹,每个文件都有很多行数据。 I want to copy the lines which only contain a certain key word, and paste every line from each file in to one master file that I can open in excel.
我想复制仅包含某个关键字的行,然后将每个文件中的每一行粘贴到一个我可以在excel中打开的主文件中。 I've been searching for an answer, and I just can't quite seem to get anything to work.
我一直在寻找答案,但似乎还无法解决任何问题。
This should do what you need. 这应该做您需要的。 Put file with this code in the directory where you have your
.log
files, replace KEYWORD
with what you are actually looking for, and run it. 将具有此代码的文件放在您的
.log
文件所在的目录中,将KEYWORD
替换为实际要查找的内容,然后运行它。
import os
theKeyword = 'KEYWORD'
directory = '.'
with open('output.csv', 'w') as out:
for file in os.listdir(directory):
if file.endswith(".log"):
with open(file, 'r') as f:
for line in f:
if theKeyword in line:
out.write(line)
As suggested, you can use glob
instead of os.listdir
: 根据建议,您可以使用
glob
代替os.listdir
:
from glob import glob
with open('output.csv', 'w') as out:
for file in glob('*.log'):
with open(file, 'r') as f:
for line in f:
if 'KEYWORD' in line:
out.write(line)
The code can be even a bit simpler if you use fileinput
module: 如果使用
fileinput
模块,代码甚至可以更简单fileinput
:
from glob import glob
import fileinput
with open('output.csv', 'w') as out:
for line in fileinput.input(glob('*.log')):
if 'KEYWORD' in line:
out.write(line)
Another variation of the 'grep in Python' thing: “ Python中的grep”的另一种形式:
from glob import glob
import fileinput
with open('output.csv', 'w') as out:
out.writelines(line for line in fileinput.input(glob('*.log')) if 'KEYWORD' in line)
In the above snippet, if you remove fileinput.input
's argument, then it will process sys.argv[1:]
, so you can run your script with file names as parameters. 在以上代码段中,如果删除
fileinput.input
的参数,则它将处理sys.argv[1:]
,因此您可以使用文件名作为参数来运行脚本。
In case you'd like to search for files recursively in subdirectories of a directory, you should have a look at os.walk
function. 如果要在目录的子目录中递归搜索文件,则应查看
os.walk
函数。
If you have a Linux/Unix/Mac box, or if you have Cygwin installed on a Windows box, the same can be achieved a bit easier using shell tools: 如果您使用的是Linux / Unix / Mac机器,或者如果您在Windows机器上安装了Cygwin ,则可以使用Shell工具轻松实现上述目的:
$ cat *.log| grep KEYWORD >output.csv
import os
outfile = open("outfile.txt", "w")
temp_list = []
for cur_file in os.listdir("."):
if cur_file.endswith(".log"):
for line in open(cur_file, "r").readlines():
if "KEYWORD" in line:
outfile.write(line)
outfile.close()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.