python删除";“许多”;文件中的行

python删除";“许多”;文件中的行,python,list,file-io,Python,List,File Io,我正试图以如下方式从python中的文件中删除特定的行号 /foo.py filename.txt 4 5 2919 其中45和2919是行号 我想做的是: for i in range(len(sys.argv)): if i>1: # Avoiding sys.argv[0,1] newlist.append(int(sys.argv[i])) 然后: 它打印原始文件中的所有行(中间有空格)您可以使用它来确定行号: import sys exclude = s

我正试图以如下方式从python中的文件中删除特定的行号

/foo.py filename.txt 4 5 2919

其中45和2919是行号

我想做的是:

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

它打印原始文件中的所有行(中间有空格)

您可以使用它来确定行号:

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)
如果从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)
如果要将内容写入文件本身,请将其写入到而不是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])
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    

您可以尝试以下方法:

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])
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    

请注意,对于临时文件的生成,最好使用module。

fileinput模块有一个
inplace=True
选项,可将stdout重定向到tempfile,该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

什么是泛型循环?如果你发布的是真代码,而不是假代码,那就更好了。我担心它可能看起来太具体了,所以我这样做了。我也鼓励你使用。如果你使用
type=int
nargs='*'
你就不必处理
sys.argv
,你可以免费得到极好的错误/使用信息;这不是线程或进程安全的;如果多个进程并行运行此程序,它们可能会覆盖临时文件。改为使用。@phihag感谢您的建议,我以前从未听说过
tempfile
。虽然
tempfile
的使用很好,
fileinput
支持在更干净的代码中进行这样简单的就地编辑