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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python@mrabarnett.plus.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.05; 'lines,': 0.07; "subject:' ": 0.07; 'subject:Error': 0.07; 'iterate': 0.09; 'parsed': 0.09; 'python': 0.11; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'int)': 0.16; 'iterating': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'received:192.168.1.4': 0.16; 'received:84.93': 0.16; 'received:84.93.230': 0.16; 'special.': 0.16; 'subject:skip:u 10': 0.16; 'wrote:': 0.18; 'file,': 0.19; 'split': 0.19; 'seems': 0.21; 'header:User-Agent:1': 0.23; 'byte': 0.24; 'text,': 0.24; 'file.': 0.24; 'code:': 0.26; 'header:In-Reply-To:1': 0.27; '[1]': 0.29; "i'm": 0.30; 'lines': 0.31; 'correctly.': 0.31; 'file': 0.32; 'text': 0.33; 'subject:from': 0.34; 'received:84': 0.35; 'but': 0.35; 'yield': 0.36; 'doing': 0.36; 'method': 0.36; 'should': 0.36; 'being': 0.38; 'christian': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'called': 0.40; "you're": 0.61; "you'll": 0.62; "you've": 0.63; 'training': 0.77; 'subject:over': 0.84
X-CM-Score 0.00
X-CNFS-Analysis v=2.1 cv=S6lXwecP c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=u9EReRu7m0cA:10 a=ji3utBqphrUA:10 a=OG2ks1k8dToA:10 a=ihvODaAuJD4A:10 a=IkcTkHD0fZMA:10 a=EBOSESyhAAAA:8 a=fGO4tVQLAAAA:8 a=1BYv3T-4GROrUg5CYpcA:9 a=QEXdDO2ut3YA:10
X-AUTH mrabarnett:2500
Date Sat, 30 Aug 2014 22:39:39 +0100
From MRAB <python@mrabarnett.plus.com>
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: Error reading from 'urllib.request' and iterating over lines
References <CAAp0bGsxWv4MGBEpAbVh7dcF2_Og75jCZHnwsKmW6yc6wMQP5Q@mail.gmail.com>
In-Reply-To <CAAp0bGsxWv4MGBEpAbVh7dcF2_Og75jCZHnwsKmW6yc6wMQP5Q@mail.gmail.com>
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13656.1409434782.18130.python-list@python.org> (permalink)
Lines 21
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1409434782 news.xs4all.nl 2934 [2001:888:2000:d::a6]:34600
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:77338

Show key headers only | 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