sys.stdout, urllib and unicode... I don't understand.

Steve Holden steve at holdenweb.com
Wed Nov 12 07:52:04 EST 2008


Thierry wrote:
> Thank you to both of you (Marc and Tino).
> 
> I feel a bit stupid right now, because as both of you said, encoding
> my source string to utf-8 do not produce an exception when I pass it
> to urllib.quote() and is what it should be.
> I was certain that this created an error sooner, and id not tried it
> again.
> The result of 2 days making random changes and hoping it works. I
> know, reflection should have primed. My bad...
> 
> The same goes for my treatment in the iteration over the request
> result.
> I now have an
>>> line=line.encode('utf-8')
> and no errors (as long as I don't try to print this to stdout, which I
> understand).
> So, I'm now really getting back an unicode string that I can handle as
> such.
> 
> I really am confused about what I was trying to do...
> I cannot understand what I did that caused those errors, because the
> state the script is now correspond to what I have in mind originally.
> 
>>> BTW: ``line.strip()`` removes all whitespace at both ends *including
>>> newlines*, so there are no '\n' to replace anymore.
> Not exactly...
> It's that I receive a string, with 2 literal characters in it: "\" and
> "n".
> What I (want to) do here is that I replace those 2 characters with 1
> chr(10).
> 
In that case you would need the following code:

ret=U''
for line in req:
  ret=ret+string.replace(line.strip(),'\\n', '\n')

Otherwise you just replace chr(10)'s with chr(10)'s, which won't help you.

Are you sure that Python wasn't just printing out "\n" because you'd
asked it to show you the repr() of a string containing newlines?

>>> And functions in the
>>> `string` module that are also implemented as method on `str` or `unicode`
>>> are deprecated.
> I actually had read that, but not modified my code.
> Thank to point it out
> 
> Anyway, thanks again to both of you.
> I'm quite happy to see it working the way I intended.

regards
 Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list