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
#1
Hello all,

I've been Googling and reading here, reading StackOverflow but can't solve what I feel is a very simple problem.

I cannot strip off the trailing '\n' from all my list items in list. I've tried a few dozen examples online and they never work for me, I then thought maybe I need something in my library but all seems well covered there too.

I'm not going to give all the failing examples here, there is no point.

Here are all the errors I'm getting using the different techniques:

AttributeError: 'list' object has no attribute 'rstrip'
AttributeError: 'list' object has no attribute 'strip'
<map object at 0x000001A36C428D30> (I know, not an error but what I got trying example)
TypeError: descriptor 'strip' for 'str' objects doesn't apply to a 'list' object
TypeError: expected string or bytes-like object
AttributeError: type object 'DataFrame' has no attribute 'res'
TypeError: 'list' object is not callable
TypeError: expected string or bytes-like object

I tried everything, I just don't get why I can't simply strip off the trailing \n in all my items.

Is there a special library for it, because everyone with their answers all seem to work.

Here is what I'm originally doing though:

res = []
with open('sample.txt', 'r') as fin:
    data = fin.read().splitlines(True)
with open('sample.txt', 'w') as fout:
    res.append(data[:200])
    fout.writelines(data[200:])
print(res)
I'm getting 200 lines from a txt file and simultaneously creating a list of the proxies to use in my program, but they all have the newlines from when they being appended.

Thanks for any help.


hmmm, I'm just now noticing the double brackets on my new list:

[['203.215.181.219:36342\n', 
'200.149.0.74:8080\n', '46.209.98.227:8080\n', '150.95.131.174:3128\n']]
Reply


Messages In This Thread
Remove all \n from end of list items - by JackMack118 - Dec-27-2019, 06:14 AM

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