I have a very unusual problem. Am working with python 2.6 to make API calls to a bulkSMS gateway. Now if I make the call like this
req = urllib2.Request(url)
urllib2.urlopen(req).read()
I get this(correct) response printed back to terminal
'<RESPONSE>\r<status>-4</status>\r<credits>31.3403</credits>\r</RESPONSE>'
But when I assign this output to variable,
reply = urllib2.urlopen(req).read()
I get this(incorrect) response when I print to terminal
print reply
</RESPONSE>.3403</credits>
Anybody care to explain what’s going on here?
It is interpreting the
\rcharacters as a special character (ASCII carriage return – see here for some examples) and removing them from the string on printing. To include them, you can use thestring-escapeencoding:Not directly applicable in your case, but another way this kind of escaping is done is to prefix a string with
r, which makes it a ‘raw’ string and keeps the backslashes (you’ll see this a lot with regular expression):