1

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.

SIM
  • 21,997
  • 5
  • 37
  • 109

1 Answers1

1

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.

blue_note
  • 27,712
  • 9
  • 72
  • 90