开发者

Compare strings with newlines in them?

开发者 https://www.devze.com 2023-01-20 07:13 出处:网络
I开发者_如何学Go\'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

I开发者_如何学Go'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.


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消