[英]Remove lines from a text file which do not contain a certain string with python
I am trying to form a quotes file of a specific user name in a log file. 我正在尝试在日志文件中形成特定用户名的报价文件。 How do I remove every line that does not contain the specific user name in it?
如何删除其中不包含特定用户名的每一行? Or how do I write all the lines which contain this user name to a new file?
或如何将包含该用户名的所有行写入新文件?
with open('input.txt', 'r') as rfp:
with open('output.txt', 'w') as wfp:
for line in rfp:
if ilikethis(line):
wfp.write(line)
with open(logfile) as f_in:
lines = [l for l in f_in if username in l]
with open(outfile, 'w') as f_out:
f_out.writelines(lines)
Or if you don't want to store all the lines in memory 或者,如果您不想将所有行存储在内存中
with open(logfile) as f_in:
lines = (l for l in f_in if username in l)
with open(outfile, 'w') as f_out:
f_out.writelines(lines)
I sort of like the first one better but for a large file, it might drag. 我有点像第一个更好,但是对于大文件,它可能会拖累。
Something along this line should suffice: 遵循以下原则即可:
newfile = open(newfilename, 'w')
for line in file(filename, 'r'):
if name in line:
newfile.write(line)
newfile.close()
See : http://docs.python.org/tutorial/inputoutput.html#methods-of-file-objects 参见: http : //docs.python.org/tutorial/inputoutput.html#methods-of-file-objects
f.readlines() returns a list containing all the lines of data in the file. f.readlines()返回一个包含文件中所有数据行的列表。
An alternative approach to reading lines is to loop over the file object. 读取行的另一种方法是遍历文件对象。 This is memory efficient, fast, and leads to simpler code
这是内存高效,快速的方法,并且可以简化代码
>>> for line in f:
print line
Also you can checkout the use of with
keyword. 您也可以检查
with
关键字的使用。 The advantage that the file is properly closed after its suite finishes 套件完成后可以正确关闭文件的优点
>>> with open(filename, 'r') as f:
... read_data = f.read()
>>> f.closed
True
I know you asked for python, but if you're on unix this is a job for grep. 我知道您要使用python,但是如果您使用的是unix,这是grep的工作。
grep name file
If you're not on unix, well... the answer above does the trick :) 如果您不在Unix上,那么...上面的答案可以解决问题:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.