简体   繁体   English

如果它们彼此相邻,如何连接不同行中的文本

[英]How to concatenate text in different lines If they are next to each other

So I have this TXT with the following sample of text:所以我有这个带有以下文本示例的 TXT:

3f064f5dc6d9054cae0602d922cfb3bc.jpg
c471d894b3384dbb5becfbf35886614c.jpg

44b703af3547891a277fe4b2971a0e8d.jpg
ca45e2c73156b8513aa4c286abac2191.jpg
ff286b1f745a235426f28f5edbdd2df6.jpg

70fe48160a3745fc9d963297d6a7e819.jpg
cb017ddb752ad8fb92f22f3cc0c0498a.jpg

I would like to concatenate all the text that is in the contiguous line into one line separated by an "OR" in python.我想将连续行中的所有文本连接成一行,在 python 中用“OR”分隔。

According to the sample above the output is 3 lines:根据上面的示例 output 是 3 行:

3f064f5dc6d9054cae0602d922cfb3bc.jpg OR c471d894b3384dbb5becfbf35886614c.jpg 3f064f5dc6d9054cae0602d922cfb3bc.jpg 或 c471d894b3384dbb5becfbf35886614c.jpg

44b703af3547891a277fe4b2971a0e8d.jpg OR ca45e2c73156b8513aa4c286abac2191.jpg OR ff286b1f745a235426f28f5edbdd2df6.jpg 44b703af3547891a277fe4b2971a0e8d.jpg 或 ca45e2c73156b8513aa4c286abac2191.jpg 或 ff286b1f745a235426f28f5edbdd2df6.jpg

70fe48160a3745fc9d963297d6a7e819.jpg OR cb017ddb752ad8fb92f22f3cc0c0498a.jpg 70fe48160a3745fc9d963297d6a7e819.jpg 或 cb017ddb752ad8fb92f22f3cc0c0498a.jpg

How can this be done?如何才能做到这一点?

Thanks in advance提前致谢

You can use itertools.groupby :您可以使用itertools.groupby

from itertools import groupby

for v, g in groupby(map(str.strip, open("your_file.txt", "r")), lambda k: k != ""):
    if v:
        print(" OR ".join(g))

Prints:印刷:

3f064f5dc6d9054cae0602d922cfb3bc.jpg OR c471d894b3384dbb5becfbf35886614c.jpg
44b703af3547891a277fe4b2971a0e8d.jpg OR ca45e2c73156b8513aa4c286abac2191.jpg OR ff286b1f745a235426f28f5edbdd2df6.jpg
70fe48160a3745fc9d963297d6a7e819.jpg OR cb017ddb752ad8fb92f22f3cc0c0498a.jpg

Just split the content by \n\n只需将内容拆分为\n\n

with open("test.txt", "r") as f:
    content = f.read()

lines = [line.rstrip().replace('\n', ' OR ') for line in content.split('\n\n')]
for line in lines:
    print(line)

3f064f5dc6d9054cae0602d922cfb3bc.jpg OR c471d894b3384dbb5becfbf35886614c.jpg
44b703af3547891a277fe4b2971a0e8d.jpg OR ca45e2c73156b8513aa4c286abac2191.jpg OR ff286b1f745a235426f28f5edbdd2df6.jpg
70fe48160a3745fc9d963297d6a7e819.jpg OR cb017ddb752ad8fb92f22f3cc0c0498a.jpg

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

相关问题 如何显示具有多行不同功能的输出以相邻打印? - How do you display outputs with multiple lines of different functions to print next to each other? 如何串联文本文件中的行? - How to concatenate lines in text file? 如何确保列表中相邻的两个数字不同 - how to make sure that two numbers next to each other in a list are different 如何将数组中的列彼此串联 - How to concatenate columns on top of each other in an array 从2个不同的行连接2个字符串(来自文本文件)-使用linecache - Concatenate 2 strings from 2 different lines (From text file) - Using linecache 将两个不同长度的列表彼此连接起来 - Concatenate two lists of different length in between each other 使用 matplotlib 在一个图表中绘制多条线但彼此相邻 - Plotting multiple lines in one chart but next to each other using matplotlib 如何在Python中连续输出多行文本(一个接一个) - How to print out multiples lines of text in a row (one next to the other) in Python 当有不同比例(双轴)的地块时,如何将两个地块并排放置? - How do I put two plots next to each other when having plots with different scales (twinaxes)? 如何在 python 的中心 uisng opencv 中用文本绘制一个彼此相邻的圆圈? - How can I draw a circle next to each other with a text in center uisng opencv in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM