Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python strip() don't work for new lines

I try to remove the '\n' after i get the data from my test file and i stile see a new lines in the output.

users_data = open('users.txt', 'r')
line = users_data.readline()
while line:
        line.strip('\n')
        metode, data = line.split(":")
        d = {} #for debuging
        d[metode]=data
        print(d)
        line = users_data.readline()

i put them in dictionary for debugging and i get :

{'gender': 'f\n'}
{'name': 'elise\n'}

Did i done something wrong?

like image 700
Or Halimi Avatar asked Oct 26 '25 14:10

Or Halimi


1 Answers

Since strings are immutable line.strip() will not do anything at all. strip() is not an inplace operation, it will return a new string.

line = line.strip()

is what you want.