Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Py3 - converting bytes to ascii

Reply
Thread Tools

Py3 - converting bytes to ascii

 
 
Anjanesh Lekshminarayanan
Guest
Posts: n/a
 
      01-15-2009
Using Python 3.0

res = urllib.request.urlopen(url)
f = open('file.txt', 'wb') # Since res.read() returns bytes
f.write(res.read())

But newline and return feeds are stored as b14, 58a as text in the text file.

So how do I to convert res.read() to ascii on opening the file in
ascii mode f = open('file.txt', 'w')?

Thanks
 
Reply With Quote
 
 
 
 
Casey
Guest
Posts: n/a
 
      01-15-2009
On Jan 15, 9:54*am, "Anjanesh Lekshminarayanan" <(E-Mail Removed)>
wrote:
> Using Python 3.0
>
> So how do I to convert res.read() to ascii on opening the file in
> ascii mode f = open('file.txt', 'w')?
>


I think this is what you are looking for:

res = urllib.request.urlopen(url)
f = open('file.txt', 'w')
f.write(res.read().decode('ascii'))
 
Reply With Quote
 
 
 
 
John Machin
Guest
Posts: n/a
 
      01-15-2009
On Jan 16, 1:54*am, "Anjanesh Lekshminarayanan" <(E-Mail Removed)>
wrote:
> Using Python 3.0
>
> res = urllib.request.urlopen(url)
> f = open('file.txt', 'wb') # Since res.read() returns bytes
> f.write(res.read())
>
> But newline and return feeds are stored as b14, 58a as text in the text file.


I can't imagine how a newline (aka line feed) and/or a carriage return
could end up being stored as "b14, 58a as text in the text file". What
are you using to view the output file?

Before you start trying to fix the problem, we need to understand it.
You can use Python itself to show you exactly what is in the file, in
a format that you can copy and paste into a news posting.

f = open('file.txt', 'rb')
data = f.read()
f.close()
print(ascii(data)) # Python 3.x
# print repr(data) # Python 2.x

> So how do I to convert res.read() to ascii on opening the file in
> ascii mode f = open('file.txt', 'w')?


Are you as sure as Casey is that your data is ASCII-only? Or are you
using "ascii" to mean "text" or "not binary"? Let's see your data
first.

Perhaps if you could give us a few clues like how much experience you
have in (Python 3.0, Python 2.x, using urllib, computer programming in
general) we could give you somewhat more focussed advice.

HTH,
John
 
Reply With Quote
 
Anjanesh Lekshminarayanan
Guest
Posts: n/a
 
      01-16-2009
The problem seems be solved with urllib.request.urlretrieve()
I think the binary information read() was giving had headers like
content-size - but not HTTP headers.
The first couple of bytes indicate how much content to read and after
reading that content, the next set of bytes indicate the next chunk.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
py3 tkinter acceps bytes. why? Matthias Kievernagel Python 6 05-08-2010 01:26 PM
py3 tkinter Text accepts what bytes? Matthias Kievernagel Python 2 04-23-2010 02:37 PM
Ratio of Bytes Delayed to Bytes Sent netproj Cisco 0 12-21-2005 08:08 PM
Private Bytes vs. # Bytes in all Heaps in Perfmon Jason Collins ASP .Net 3 02-18-2004 03:59 PM
Re: receiving Bytes and sending Bytes The Old Sourdough Computer Support 0 07-23-2003 01:23 PM



Advertisments