如何在Python中将特定行写入文件?

0 投票
5 回答
1699 浏览
提问于 2025-04-16 02:28

我有一个文件,内容大概是这样的(为了让它看起来像文件,所以放在代码框里):

text
(starts with parentheses)
         tabbed info
text
(starts with parentheses)
         tabbed info

...repeat

我想从这个文件中只提取“文本”行(或者说每第四行),然后把它们复制到另一个文件里。下面是我写的代码,但它却把所有内容都复制到了新文件里:

import sys

def process_file(filename):

    output_file = open("data.txt", 'w')

    input_file = open(filename, "r")
    for line in input_file:
        line = line.strip()
                if not line.startswith("(") or line.startswith(""):
                        output_file.write(line)        
    output_file.close()
if __name__ == "__main__":
process_file(sys.argv[1])

5 个回答

1

你的脚本会复制每一行的原因是因为 line.startswith("") 这个条件总是为真,不管 line 的内容是什么。

你可以试试用 isspace 来检查 line 是否以空格开头:

def process_file(filename):
    with open("data.txt", 'w') as output_file:
        with open(filename, "r") as input_file:
            for line in input_file:
                line=line.rstrip()
                if not line.startswith("(") or line[:1].isspace():
                    output_file.write(line) 
1
with open('data.txt','w') as of:
    of.write(''.join(textline
                     for textline in open(filename)
                     if textline[0] not in ' \t(')
             )

要写每第四行,可以用切片方法 result[::4]。

with open('data.txt','w') as of:
    of.write(''.join([textline
                     for textline in open(filename)
                     if textline[0] not in ' \t('][::4])
             )

我不需要去掉末尾的换行符,因为我在写的时候会用到它们。

0

除了 line.startswith("") 总是返回真以外,line.strip() 这个方法会去掉行首的制表符,这样就能把带有制表符的数据也写出来。你可以把它改成 line.rstrip(),然后用 \t 来检查是否有制表符。你代码的那部分应该看起来像这样:

line = line.rstrip()
if not line.startswith(('(', '\t')):
    #....

关于你在评论中提到的问题:

#edited in response to comments in post
for i, line in input_file:
    if i % 4 == 0:
        output_file.write(line)

撰写回答