![](/img/trans.png)
[英]When printing out lines from a file, in the python executor it has a line gap between each line
[英]Printing links from a text file produces "line gap" in between
當我打印存儲在text
文件中的鏈接時,我得到的結果總是有點奇怪,在這種情況下,每個鏈接之間有一個行距。 如果我使用.strip()
那么它可以解決問題,但它的使用似乎並不令人滿意。 我的問題是:除了使用.strip()
之外,我怎樣才能獲得通常的結果? 提前致謝。
這是我嘗試過的:
with open('reverse.txt', 'r') as file:
for item in file.readlines():
print(item)
print(item.strip()) # ain't there any better way? It fixes, though.
結果:
http://metaladhesive.tradeindia.com/
http://www.iari.res.in/
http://www.peekayfarmequipments.com/
我期待的是:
http://metaladhesive.tradeindia.com/
http://www.iari.res.in/
http://www.peekayfarmequipments.com/
順便說一句,同樣的事情也適用於csv
文件。
readlines()
保留換行符。 您可以改為使用file.read().splitlines()
,其中read()
將整個文件作為字符串返回(帶換行符),而splitlines()
是str
一種方法,可以刪除它們。
但是,我認為您應該保持代碼原樣。 讀完后把線去掉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.