简体   繁体   中英

Printing links from a text file produces "line gap" in between

When I print links stored in a text file, the result I'm getting constantly kinda weird which is in this case a line gap between each link. If i use .strip() then it fixes the problem but it doesn't seem satisfactory usage. My question is: how can i get usual results other than using .strip() ? Thanks in advance.

This is what I tried:

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.

The results:

http://metaladhesive.tradeindia.com/

http://www.iari.res.in/

http://www.peekayfarmequipments.com/

What I'm expecting:

http://metaladhesive.tradeindia.com/
http://www.iari.res.in/
http://www.peekayfarmequipments.com/

Btw, the same thing applicable for csv files as well.

readlines() retains the line break. You could instead use file.read().splitlines() , where read() returns the whole file as a string (with newlines), and splitlines() is a method of str which removes them.

However, I think you should keep your code as it is. Just strip the line after you read it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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