繁体   English   中英

如何用 Python 中包含相同数据的单行文本替换文件中的多个 3 行文本块

[英]How can I replace multiple 3 line blocks of text in a file with a single line of text containing the same data in Python

我有一个文本文件,其中包含多个 3 行文本块,后跟一个新行。 我的数据如下所示:

title A - description
http://www.a.site.com/
http://a.anothersite.com/

title B - blah blah
http://www.site.b.com/
http://b.anothersite.com/

title C - yeah yeah
http://www.site.c.com/
http://anothersite.c.com/

我希望实现的输出是这样的:

title A - description | http://www.a.site.com/ | http://a.anothersite.com/   
title B - blah blah | http://www.site.b.com/ | http://b.anothersite.com/
title C - yeah yeah | http://www.site.c.com/ | http://anothersite.c.com/

我一直在尝试用 python 来做到这一点,但我并没有真正取得任何进展。 我能够完成的最好的事情是删除所有新行,但这在这种情况下并没有真正帮助,因为我仍然需要在每条数据之间添加一条新行。 有什么建议?

three_lines_joined = ''
strings_to_join = []
results = []


for index, item in enumerate(text):
    if item is not '\n':
        strings_to_join.append(item.strip())
    else:
        three_lines_joined = ' | '.join(strings_to_join)
        results.append(three_lines_joined)
        three_lines_joined = ''
        strings_to_join = []

这是我使用正则表达式和替换的解决方案

import re

text = """
title A - description
http://www.a.site.com/
http://a.anothersite.com/

title B - blah blah
http://www.site.b.com/
http://b.anothersite.com/

title C - yeah yeah
http://www.site.c.com/
http://anothersite.c.com/
"""

text = text.strip()
text = re.sub('[^\n](\n)[^\n]', ' | ', text).replace('\n\n', '\n')

print(text)

尝试这个:

import re
with open("file.txt", "r+") as f:
    text = " | ".join(f.readlines())
    text = re.sub(r"(?<!^)\n", '', text)
    text = re.sub(r"\s*\|\s*\|\s*", "\n", text)

    f.seek(0)
    f.write(text)

file.txt输出:

title A - description | http://www.a.site.com/ | http://a.anothersite.com/
title B - blah blah | http://www.site.b.com/ | http://b.anothersite.com/
title C - yeah yeah | http://www.site.c.com/ | http://anothersite.c.com/

首先像您一样删除空行,然后使用lines = fulltext.split("\\n")获取行列表。 然后运行如下:

for i in range(len(lines))/3:
    title, desc = lines[3*a].split("-")
    website1, website2 = lines[3*a+1], lines[3*a+2]
    print(title + " - " + desc + " | " + website1 + " - " + website2)

这也允许您在代码中使用变量。 如果你真的只想要一个文本输出,那么看看你的输入试试:

fulltext.replace("\n"," ")

这应该会产生您想要的文本输出(可能需要稍作修改)。 但是,我更推荐第一个版本,因为它可以让您稍后将这些值用于其他用途。 变量通常比格式化的文本文档更有用。

暂无
暂无

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

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