Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #77338

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

Date 2014-08-30 22:39 +0100
From MRAB <python@mrabarnett.plus.com>
Subject Re: Error reading from 'urllib.request' and iterating over lines
References <CAAp0bGsxWv4MGBEpAbVh7dcF2_Og75jCZHnwsKmW6yc6wMQP5Q@mail.gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.13656.1409434782.18130.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Error reading from 'urllib.request' and iterating over lines MRAB <python@mrabarnett.plus.com> - 2014-08-30 22:39 +0100

csiph-web