tags:

views:

85

answers:

2

I'm trying to develop a script which compares a runtime generated string against one which is input by the user. Unfortunately, since user inputs his code using a textbox, I get ^M in the string input by user. Example, If I print these strings to file I get this:

User Input:
1^M 
2^M
3

Output of script:
1
2
3

Obviously, when I try to compare these two strings, I get a false. What would be the most efficient way to sort this problem? Just in case you haven't noticed, all lines entered by user "donot" end with "^M". The last line in this user string has no ^M, while output of code has a "\n" at the end. Thanks in advance.

+5  A: 

You should use str.splitlines() to split the text coming from the textbox, instead of whatever it is you're using now. That method handles \r\n properly.

Ignacio Vazquez-Abrams
A: 

mystring.rstrip('\r') will return a new string with any ^M removed from the end of the string.

Mark Ransom
Those lines are not separate strings. Actually, I'm trying to compare "1^M\n2^M\n3" with "1\n2\n3\n".
Neo
@Neo: Well there's your problem. Split them into lists of lines first, then compare them.
Ignacio Vazquez-Abrams