-1

Hi I have a text file which has the following:

'username=sonia\n', 'password=password'

I want to split sonia and place it in a variable called username.

I was able to accomplish the following but could not remove the new line characters.

text = 'username=sonia\n', 'password=password'

text[0].split('=')[1]

This returns 'sonia\n'

So now how do I get rid of the \n?

What should be modified to also remove the new line '\n'?


Thanks in advance.

Sonia

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by sonial8
0

If you know that there will always be a newline there, you could just splice the string that is returned.

text[0].split('=')[1][:-1]

that should work. This takes everything but the last char which is the newline.

-Michael

0

hi,
another way is to use the .strip() method like this:

>>> n = "sonya\n"
>>> print n
sonya

>>> a = n.strip()
>>>print a
sonya
>>>

.strip() also removes leading or trailing whitespaces

hope this helps :)

0

Micheal, Nice, very nice, this worked.

Thanks,
Sonia

Edited by sonial8: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.