读取和附加的具体内容 [英] Read and Append the specific content

查看:99
本文介绍了读取和附加的具体内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

后来到我的previous 的问题,如果我想什么附加线。在这种情况下,阻止他人在输出文件中指定。

Subsequent to my previous question what if I want to append line. In that case others block are also specified in output file.

输入文件FILE1.TXT

##### Xyz
* [] Task 112
* [] Cl 221

##### Foo
* [] Task 1
* [x] Clone 2

##### Bar:
* [x] Email to A
* [] Email to B
* [x] Email to C

##### Bob
* [] Task 3
* [x] Clone Bob

OUTPUTFILE FILE2.TXT

##### Xyz


##### Foo
* [x] Clone 2

##### Bar:
* [x] Email to A
* [x] Email to C

##### Bob
* [x] Clone Bob

输出文件中所示的输出文件predefined结构 - FILE2.TXT与指定块。如果一些新的项目内容* [X]输入被添加的文件应在输出文件追加,不得添加任何重复的项目。看到argparse的使用情况和一个特定的块或整个块开始追加与[X]这将是有趣。感谢:)

The output file is predefined structure as shown in output file - file2.txt with specified blocks. If some new items content * [x] are added in input file it shall append in output file, shall not add any duplicate items. It would be interesting to see the usage of argparse and append one specific block or entire block start with [x]. Thanks :)

推荐答案

从输入读取文件中的所有行,但只写那些以指定的字符串开始:

Read all lines from the input file but write only those that start with specified strings:

with open("file1.txt", "rt") as finp:
    with open("file2.txt", "wt") as fout:
        for line in finp.readlines():
            if line.startswith("#####") or line.startswith("* [x]"):
                fout.write(line)

这篇关于读取和附加的具体内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆