簡體   English   中英

如何按特定順序組合文本文件中的行?

[英]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)

這是該程序的 output,使用上面的示例輸入文件:

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

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

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

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

我的代碼有什么問題?

為了解決這個問題,我們需要為 sitex 實現rstrip()方法,以刪除字符串末尾的換行符。 (歸功於BrokenBenchmark

其次,打印命令每次調用時默認換行,所以我們必須添加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

Output:

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

看:

[chars]是可選的——如果沒有給出,空格將被刪除。

如果它不包含'https' ,則需要從該行中刪除尾隨換行符:

sitex = two

應該

sitex = two.rstrip()

正如ShadowRanger指出的那樣,您還需要為else塊做類似的事情:

print (sitex + "-" +two)

應該

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM