Py3 - converting bytes to ascii

Discussion in 'Python' started by Anjanesh Lekshminarayanan, Jan 15, 2009.

  1. 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
     
    Anjanesh Lekshminarayanan, Jan 15, 2009
    #1
    1. Advertising

  2. Anjanesh Lekshminarayanan

    Casey Guest

    On Jan 15, 9:54 am, "Anjanesh Lekshminarayanan" <>
    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'))
     
    Casey, Jan 15, 2009
    #2
    1. Advertising

  3. Anjanesh Lekshminarayanan

    John Machin Guest

    On Jan 16, 1:54 am, "Anjanesh Lekshminarayanan" <>
    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
     
    John Machin, Jan 15, 2009
    #3
  4. 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.
     
    Anjanesh Lekshminarayanan, Jan 16, 2009
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Collins
    Replies:
    3
    Views:
    6,026
    Jason Collins
    Feb 18, 2004
  2. mrby

    4-bytes or 8-bytes alignment?

    mrby, Nov 2, 2004, in forum: C Programming
    Replies:
    8
    Views:
    424
    Mark McIntyre
    Nov 2, 2004
  3. Replies:
    5
    Views:
    553
    Flash Gordon
    Apr 9, 2006
  4. Matthias Kievernagel

    py3 tkinter Text accepts what bytes?

    Matthias Kievernagel, Apr 23, 2010, in forum: Python
    Replies:
    2
    Views:
    330
    Matthias Kievernagel
    Apr 23, 2010
  5. Matthias Kievernagel

    py3 tkinter acceps bytes. why?

    Matthias Kievernagel, May 4, 2010, in forum: Python
    Replies:
    6
    Views:
    317
    Martin v. Loewis
    May 8, 2010
Loading...

Share This Page