Bottom Page

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']]
Quote
#2
You can simply strip newline from the end of the string. But is this actually needed?
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.
Quote
#3
Hello yes because I set it as a proxy for requests. I've been doing all the strip methods, string strip methods and keep getting all the errors I mentioned in the OP.

I think it's because it's not turning into a list because of the double [[]], that's why the stripping is bringing errors.

OK, I have a working example but I open file 3 times which is probably not a good idea for speed. What I need to do is get 200 lines from a text file into a list, and delete those 200 lines simultaneously.

Anyway this works, but I think it's not best:

with open("sample.txt") as myfile:
    res = [next(myfile).strip() for x in range(200)]
with open('sample.txt', 'r') as fin:
    data = fin.read().splitlines(True)
with open('sample.txt', 'w') as fout:
    fout.writelines(data[200:])
print(res)

Ok, keep answering my own post, I've slimmed down, this works best.

with open('sample.txt', 'r') as fin:
    res = [next(fin).strip() for x in range(200)]
    data = fin.read().splitlines(True)
with open('sample.txt', 'w') as fout:
    fout.writelines(data[200:])
print(res)
Quote
#4
You are doing two task here making a list and wirte to file.
Let say this is sample.txt.
Output:
203.215.181.219:36342 200.149.0.74:8080 46.209.98.227:8080 150.95.131.174:3128
Now just do the witting to file part and it seems okay.
with open('sample.txt', 'r') as fin:
    data = fin.read().splitlines(True)
with open('sample1.txt', 'w') as fout:
    fout.writelines(data[2:])
Other name sample1.txt so it don't overwrite original in this test.
Output:
46.209.98.227:8080 150.95.131.174:3128
Getting a list without \n could be done like this.
with open('sample.txt', 'r') as fin:
    data = [i.strip() for i in fin]
Output:
>>> data ['203.215.181.219:36342', '200.149.0.74:8080', '46.209.98.227:8080', '150.95.131.174:3128']
Writing back from this list could be.
with open('sample.txt', 'r') as fin:
    data = [i.strip() for i in fin]
with open('sample2.txt', 'w') as fout:
    for ip in data[:2]:
        fout.write(f'{ip}\n')
Output:
203.215.181.219:36342 200.149.0.74:8080
JackMack118 likes this post
Quote
#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).
JackMack118 likes this post
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.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  How to put the items of one list in new generated lists Bobbear 1 121 Jun-12-2020, 06:08 AM
Last Post: buran
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 319 May-10-2020, 05:28 AM
Last Post: deanhystad
  mydict.items() is not a list Skaperen 2 216 May-07-2020, 07:03 AM
Last Post: DeaD_EyE
  Add items from one list to list of lists PUP280 5 582 May-05-2020, 03:47 PM
Last Post: PUP280
  Can't seem to figure out how to put all of the lists items from a loop into 1 list Cosmosso 4 481 Feb-21-2020, 02:40 PM
Last Post: Cosmosso
  counting items in a list of number combinations Dixon 2 265 Feb-19-2020, 07:06 PM
Last Post: Dixon
  List items verification for Integer type vintysaw 4 368 Jan-17-2020, 01:56 PM
Last Post: perfringo
  Access list items in Python kamaleon 2 426 Dec-31-2019, 11:10 AM
Last Post: kamaleon
  Removing duplicate list items eglaud 4 357 Nov-22-2019, 08:07 PM
Last Post: ichabod801
  Adding items in a list (using loops?) Seneca260 6 496 Nov-22-2019, 11:34 AM
Last Post: Seneca260

Forum Jump:


Users browsing this thread: 1 Guest(s)