Error reading from 'urllib.request' and iterating over lines

MRAB python at mrabarnett.plus.com
Sat Aug 30 17:39:39 EDT 2014


On 2014-08-30 21:16, Juan Christian wrote:
> My code: http://pastebin.com/CBgVvT4n
>
> Line 25 returns the text correctly [1], but it seems not being parsed
> to line 27-28 correctly. This is just a training program that I'm
> doing following some books/tutorials/docs, nothing special.
>
> [1] Output from line 25: http://pastebin.com/HSbAtDHQ
>
> Python 3.4.1
> PySide 1.2.2
>
You've called it 'fh', which suggests that it's a file. It isn't. It's
a bytestring (what the .read method return).

If you iterate over a file that's been opened as text, you'll get the
lines of the file, but you're iterating over a bytestring, which will
yield each byte (an int) in turn.

You should decoding the bytestring, split it into lines, and then
iterate over them.



More information about the Python-list mailing list