Python Forum
Remove all \n from end of list items
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove all \n from end of list items
#5
Another possibility is to use split('\n') instead of splitlines.

Using data provided by snippsat in file named ip.txt:

>>> with open('ip.txt', 'r') as f: 
...     data = f.read().split('\n') 
...
>>> data
['203.215.181.219:36342',
 '200.149.0.74:8080',
 '46.209.98.227:8080',
 '150.95.131.174:3128']
I still don't understand why it's good idea to strip newlines while reading the file and then put them back while writing to file (I know, I am slow today).
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
RE: Remove all \n from end of list items - by perfringo - Dec-27-2019, 08:34 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Remove double quotes from the list ? PythonDev 22 676 Nov-05-2020, 04:53 PM
Last Post: snippsat
  Count number of occurrences of list items in list of tuples t4keheart 1 118 Nov-03-2020, 05:37 AM
Last Post: deanhystad
  concatenating 2 items at a time in a python list K11 3 171 Oct-21-2020, 09:34 AM
Last Post: buran
  Remove specific elements from list with a pattern Xalagy 3 344 Oct-11-2020, 07:18 AM
Last Post: Xalagy
  How to remove dict from a list? Denial 7 281 Sep-28-2020, 02:40 PM
Last Post: perfringo
  Select the other of 2 items in a list Clunk_Head 7 460 Sep-01-2020, 05:27 PM
Last Post: Clunk_Head
  Removing items from list if containing a substring pythonnewbie138 2 367 Aug-27-2020, 10:20 PM
Last Post: pythonnewbie138
  Print the number of items in a list on ubuntu terminal buttercup 2 436 Jul-24-2020, 01:46 PM
Last Post: ndc85430
  How to put the items of one list in new generated lists Bobbear 1 321 Jun-12-2020, 06:08 AM
Last Post: buran
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 582 May-10-2020, 05:28 AM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020