简体   繁体   English

python从文件中删除“many”行

[英]python remove “many” lines from file

I am trying to remove specific line numbers from a file in python in a way such as 我试图以某种方式从python中的文件中删除特定的行号

./foo.py filename.txt 4 5 2919 ./foo.py filename.txt 4 5 2919

Where 4 5 and 2919 are line numbers 其中4 5和2919是行号

What I am trying to do is: 我想要做的是:

for i in range(len(sys.argv)):
    if i>1: # Avoiding sys.argv[0,1]
        newlist.append(int(sys.argv[i]))

Then: 然后:

count=0

generic_loop{ 
   bar=file.readline()
   count+=1
   if not count in newlist:
      print bar
}

it prints all the lines in original file (with blank spaces in between) 它打印原始文件中的所有行(中间有空格)

You can use enumerate to determine the line number: 您可以使用enumerate来确定行号:

import sys
exclude = set(map(int, sys.argv[2:]))
with open(sys.argv[1]) as f:
    for num,line in enumerate(f, start=1):
        if num not in exclude:
            sys.stdout.write(line)

You can remove start=1 if you start counting at 0. In the above code, the line numbering starts with 1: 如果从0开始计数,则可以删除start=1在上面的代码中,行编号从1开始:

$ python3 so-linenumber.py so-linenumber.py 2 4 5
import sys
with open(sys.argv[1], 'r') as f:
            sys.stdout.write(line)

If you want to write the content to the file itself, write it to a temporary file instead of sys.stdout, and then rename that to the original file name (or use sponge on the command-line), like this: 如果要将内容写入文件本身,请将其写入临时文件而不是sys.stdout,然后重命名为原始文件名(或在命令行中使用海绵 ),如下所示:

import os
import sys
from tempfile import NamedTemporaryFile
exclude = set(map(int, sys.argv[2:]))
with NamedTemporaryFile('w', delete=False) as outf:
    with open(sys.argv[1]) as inf:
        outf.writelines(line for n,line in enumerate(inf, 1) if n not in exclude)
    os.rename(outf.name, sys.argv[1])

You can try something like this: 你可以尝试这样的事情:

import sys
import os
filename= sys.argv[1]
lines = [int(x) for x in sys.argv[2:]]

#open two files one for reading and one for writing

with open(filename) as f,open("newfile","w") as f2:

#use enumerate to get the line as well as line number, use enumerate(f,1) to start index from 1
    for i,line in enumerate(f):  
        if i not in lines:     #`if i not in lines` is more clear than `if not i in line`
            f2.write(line)   
os.rename("newfile",filename)  #rename the newfile to original one    

Note that for the generation of temporary files it's better to use tempfile module. 请注意,对于临时文件的生成,最好使用tempfile模块。

import sys
# assumes line numbering starts with 1
# enumerate() starts with zero, so we subtract 1 from each line argument
omitlines = set(int(arg)-1 for arg in sys.argv[2:] if int(arg) > 0)
with open(sys.argv[1]) as fp:
    filteredlines = (line for n,line in enumerate(fp) if n not in omitlines)
    sys.stdout.writelines(filteredlines)

The fileinput module has an inplace=True option that redirects stdout to a tempfile which is automatically renamed after for you. fileinput模块有一个fileinput inplace=True选项,可以将stdout重定向到一个tempfile,后面会自动重命名。

import fileinput
exclude = set(map(int, sys.argv[2:]))

for i, line in enumerate(fileinput.input('filename.txt', inplace=True), start=1):
    if i not in exclude:
        print line, # fileinput inplace=True redirects stdout to tempfile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM