4

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?

Or Halimi
  • 671
  • 3
  • 10
  • 19
  • Of course you normally don't use `readline()`. Just use `for line in users_data:`. – Matthias Oct 04 '13 at 08:42
  • why not? because it will store all the file in the memory? my pogram read some lines and create a users. then read more lines and create more users. with read line i can track this. – Or Halimi Oct 04 '13 at 09:57
  • 2
    It's just so "old-style" and contains more code. You can just loop over the file object. Have a look at "[Methods of File Objects](http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects)" in the documentation. – Matthias Oct 04 '13 at 11:40
  • but the question is, if i will loop untill the middle of the file and then end the loop (wont close the file). and then i will start the loop again - it will start from the beginning? – Or Halimi Oct 05 '13 at 04:12

1 Answers1

4

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.