简体   繁体   English

使用python从文本文件中删除不包含某些字符串的行

[英]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.

相关问题 如何在 Python 中打印包含某个字符串的文本文件的行? - How to print the lines of a text file that contain a certain string in Python? 在 python 中,如何打印不包含某个字符串的行,而不是打印包含某个字符串的行: - In python, how can I print lines that do NOT contain a certain string, rather than print lines which DO contain a certain string: 删除包含特定字符串的行 - Remove lines that contain certain string python从包含NA的文本中删除行 - python remove lines from text that contain NA 查找文件中的哪些行包含特定字符 - Find which lines in a file contain certain characters 如何从巨大的 txt 文件中删除/删除与文本匹配的特定行 - How to delete/remove a certain set of lines which matches the text from a huge txt file 有没有办法从 Python 中的文本文件中删除整个行的部分,然后删除其余部分的某些部分? - Is there a way to remove entire parts of lines from a text file in Python, then remove certain parts of the rest? 如果字符串不包含 Python 中的某些字符,我如何从列表中删除它 - How do i remove a string from a list if it DOES NOT contain certain characters in Python 列出python中文本文件中的某些行 - List certain lines from a text file in python 过滤文本文件中包含特定单词的行 - Filter lines from a text file which contain a particular word
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM