如何删除文本文件中关键短语之前的所有内容?

时间:2013-01-10 19:58:45

标签: python

我有超过5000个文本文件(也是csv格式),每个文件都有几百行。

在特定短语之上的所有内容,“城市”都是不必要的,我需要它下面的所有内容,有没有办法(python或批处理)删除所有内容?

6 个答案:

答案 0 :(得分:5)

python。但有时,sed也很有用:

sed -n '/City/,$p' file_with_city > new_file_with_city_on_first_line

答案 1 :(得分:2)

一种算法就是:

  • 从文件中读取,直至遇到文本“City”
  • 以写入模式打开第二个文件
  • 从第一个文件流到第二个
  • 关闭两个文件
  • 将第二个文件移至先前由第一个
  • 占据的位置

虽然可以截断文件以在某个点之后删除内容,但是在某个点之前无法使用内容对其进行大小调整。你可以通过反复寻找来回使用单个文件,但这可能不值得。

如果文件足够小,你可以将整个第一个文件读入内存,然后将你想要的部分写回同一个磁盘文件。

答案 2 :(得分:2)

Python中的

sed -i -n '/City/,$p' file1 file2 etc模拟:

#!/usr/bin/env python
import fileinput

copy = False
for line in fileinput.input(inplace=True): # edit files inplace
    if fileinput.isfirstline() or not copy: # reset `copy` flag for next file
       copy = "City" in line
    if copy:
       print line, # copy line

用法:

$ ./remove-before-city.py file1 file2 etc

此解决方案会修改在命令行中提供的文件。

答案 3 :(得分:1)

# Use a context manager to make sure the files are properly closed.
with open('in.csv', 'r') as infile, open('out.csv', 'w') as outfile:
    # Read the file line by line...
    for line in infile:
        # until we have a match.
        if "City" in line:
            # Write the line containing "City" to the output.
            # Comment this line out if you don't want to include it.
            outfile.write(line)

            # Read the rest of the input in one go and write it
            # to the output. If you file is really big you might
            # run out of memory doing this and have to break it
            # into chunks.
            outfile.write(infile.read())

            # Our work here is done, quit the loop.
            break

答案 4 :(得分:0)

def removeContent(file, word, n=1, removeword=False):
    with open(fname, "r") as file:
        if removeword:
            content = ''.join(file.read().split(word, n)[n])
        else:
            content = word + ''.join(file.read().split(word, n)[n])
    with open(fname, "w") as file:
        file.write(content)

for fname in filenames:
    removeContent(fname)

参数说明:

n告诉我们您要将其用于删除的单词出现。默认情况下为n = 1,因此它会在FIRST发生之前删除所有内容。要在第五个city之前删除evertything,请使用removeContent(fname, "city", 5)调用该函数。

file显然代表您要编辑的文件的名称

word是您要用于删除的字词,在您的情况下,它将是city

removeword告诉他们保留这个词,只删除它之前的文字,或者是否也删除这个词本身。

答案 5 :(得分:0)

import os

for file in os.listdir("."):
    infile = open(file, 'rb')
    line = infile.readline()
    # Sequential read is easy on memory if the file is huge.
    while line != '' and not 'City' in line:
        line = infile.readline()     # skip all lines till 'City' line
    # Process the rest of the file after 'City'
    if 'City' in line:
        print line     # prints to stdout (or redirect to outfile)
    while line != '' :
        line = infile.readline()
        print line