3

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?

2 Answers 2

3

It is interpreting the \r characters 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 the string-escape encoding:

In [1]: resp = '<RESPONSE>\r<status>-4</status>\r<credits>31.3403</credits>\r</RESPONSE>'

In [2]: print resp
</RESPONSE>.3403</credits>

In [3]: print resp.encode('string-escape')
<RESPONSE>\r<status>-4</status>\r<credits>31.3403</credits>\r</RESPONSE>

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):

In [4]: resp = r'<RESPONSE>\r<status>-4</status>\r<credits>31.3403</credits>\r</RESPONSE>'

In [5]: print resp
<RESPONSE>\r<status>-4</status>\r<credits>31.3403</credits>\r</RESPONSE>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this worked for me, while still preserving the content of the variable in it's original form.
@user1048839 Awesome, glad it worked out for you. Good luck with everything!
2

The string contains the correct data, it's just printing in a weird way. \r is a carriage return, which some systems interpret as "go back to the beginning of the line and start overwriting from there". So you see "</RESPONSE>" plus data from the previous line that was longer (and so wasn't overwritten).

It's a bit odd that your data contain \r since it's not that common as a line-ending convention these days.

You could print repr(response) to see the response without \r interpreted this way. Any processing you do on the line should work fine since all the data is there, it's just not printing.

3 Comments

+1 - good point about the resetting of the position back to the beginning of the line.
Yep, this is exactly the issue. If necessary, you can fix the string with reply.replace("\r", "\n"), which will put newlines in place of the carriage returns.
Am very tempted to replace the offending character 'r' but that would essentially mean changing the content of the reply which I would like to stay as close to original as possible since I will be using it across other functions too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.