如何按特定顺序组合文本文件中的行?

How do I combine lines in a text file in a specific order?

我正在尝试根据以下规则转换文件中的文本:对于每一行,如果该行不是以“https”开头,则将该单词添加到后续行的开头,直到您找到另一行使用非 https 字词。

例如,给定此文件:

Fruit
https://www.apple.com//
https://www.banana.com//
Vegetable
https://www.cucumber.com//
https://www.lettuce.com//

我要

Fruit-https://www.apple.com//
Fruit-https://www.banana.com//
Vegetable-https://www.cucumber.com//
Vegetable-https://www.lettuce.com//

这是我的尝试:

one = open("links.txt", "r")
for two in one.readlines():

    if "https" not in two:
        sitex = two
        
    else:
        print (sitex + "-" +two)

这是该程序的输出,使用上面的示例输入文件:

Fruit
-https://www.apple.com//

Fruit
-https://www.banana.com//       

Vegetable
-https://www.cucumber.com//     

Vegetable
-https://www.lettuce.com//   

我的代码有什么问题?

如果不包含 'https':

,您需要从行中删除尾随的换行符
sitex = two

应该是

sitex = two.rstrip()

您也需要为 else 块做类似的事情,正如 ShadowRanger 指出的那样:

print (sitex + "-" +two)

应该是

print (sitex + "-" + two.rstrip())

要解决这个问题,我们需要对 sitex 执行 rstrip() 方法以删除字符串末尾的换行符。 (归功于

其次,打印命令每次调用时默认换行,所以我们必须添加 end="" 参数来解决这个问题。

所以你的代码应该是这样的

one = open("links.txt", "r")
for two in one.readlines():
    if "https" not in two:
        sitex = two.rstrip()
    else:
        print (sitex + "-" +two,end="")
one.close()

也总是在完成后关闭文件。

文件中的行结束于 "\n" - 换行符。

您可以使用 strip()(两端)或 rstrip() / lstrip()(在一端删除)从字符串中删除空格(包括 "\n")。

print() 默认在末尾添加一个"\n",你可以使用

省略这个
print("something", end=" ")
print("more)   # ==> 'something more' in one line

修复您的代码:

# use a context handler for better file handling
with open("data.txt","w") as f:
    f.write("""Fruit
https://www.apple.com//
https://www.banana.com//
Vegetable
https://www.cucumber.com//
https://www.lettuce.com//
""")


with open("data.txt") as f:
    what = ""
    # iterate file line by line instead of reading all at once
    for line in f:
        # remove whitespace from current line, including \n
        # front AND back - you could use rstring here as well
        line = line.strip() 
        # only do something for non-empty lines (your file does not
        # contain empty lines, but the last line may be empty
        if line:
            # easier to understand condition without negation
            if line.startswith("http"):
                # printing adds a \n at the end
                print(f"{what}-{line}") # line & what are stripped
            else:
                what = line

输出:

Fruit-https://www.apple.com//
Fruit-https://www.banana.com//
Vegetable-https://www.cucumber.com//
Vegetable-https://www.lettuce.com//

参见:

[chars] 是可选的 - 如果没有给出,空格将被删除。