开发者

Remove lines from a text file which do not contain a certain string with python

开发者 https://www.devze.com 2023-01-27 07:46 出处:网络
I am trying to form a quote开发者_StackOverflow中文版s 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 th

I am trying to form a quote开发者_StackOverflow中文版s 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

f.readlines() returns a list containing all the lines of data in the file.

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. 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.

grep name file

If you're not on unix, well... the answer above does the trick :)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号