FAQ
Hi all:

I am trying to read a non-text file as a string by using Python
read(), however, it seems there is some thing wrong with it. I can use
read() on text file correctly, but unable to read .xls file correctly.
(The program can read any file correctly in Fedora 10)

Any idea how to solve this problem?

Thank you very much!

--
Li
------
Time is all we have
and you may find one day
you have less than you think

Search Discussions

  • Chris Rebert at May 8, 2009 at 5:38 am

    On Thu, May 7, 2009 at 10:04 PM, Li Wang wrote:
    Hi all:

    I am trying to read a non-text file as a string by using Python
    read(), however, it seems there is some thing wrong with it. I can use
    read() on text file correctly, but unable to read .xls file correctly.
    (The program can read any file correctly in Fedora 10)

    Any idea how to solve this problem?
    You need to add a "b" to the `flags` argument to open() when you open
    the file, e.g. open("the_file.xls", "rb")
    Unlike *nix, Windows differentiates between binary and text files,
    hence the need for the "b" flag to specify which you're dealing with.

    Further info: http://docs.python.org/library/functions.html#open

    Cheers,
    Chris
  • Li Wang at May 8, 2009 at 8:14 am

    2009/5/8 Chris Rebert <clp2 at rebertia.com>:
    On Thu, May 7, 2009 at 10:04 PM, Li Wang wrote:
    Hi all:
    the file, e.g. open("the_file.xls", "rb")
    Unlike *nix, Windows differentiates between binary and text files,
    hence the need for the "b" flag to specify which you're dealing with.
    Hi
    Thank you very much for reply,

    The method doesn't work.
    Here is the problem: after reading the whole fie as a string, I need
    the string to be made of 8bits-symbols.

    I do not mind what's the content in the file, what I need to do is
    something like reading the file byte by byte and concatenate these
    bytes into a single string.

    Any suggestions?

    Thank you very much





    --
    Li
    ------
    Time is all we have
    and you may find one day
    you have less than you think
  • Li Wang at May 8, 2009 at 9:04 am
    Hi:

    Problem solved

    Thank you very much, it works, It is my own problem:)

    All the best,
    Li

    2009/5/8 Li Wang <li.wang.d at gmail.com>:
    2009/5/8 Chris Rebert <clp2 at rebertia.com>:
    On Thu, May 7, 2009 at 10:04 PM, Li Wang wrote:
    Hi all:
    the file, e.g. open("the_file.xls", "rb")
    Unlike *nix, Windows differentiates between binary and text files,
    hence the need for the "b" flag to specify which you're dealing with.
    Hi
    Thank you very much for reply,

    The method doesn't work.
    Here is the problem: after reading the whole fie as a string, I need
    the string to be made of 8bits-symbols.

    I do not mind what's the content in the file, what I need to do is
    something like reading the file byte by byte and concatenate these
    bytes into a single string.

    Any suggestions?

    Thank you very much





    --
    Li
    ------
    Time is all we have
    and you may find one day
    you have less than you think


    --
    Li
    ------
    Time is all we have
    and you may find one day
    you have less than you think
  • Dave Angel at May 8, 2009 at 7:25 am

    Li Wang wrote:
    Hi all:

    I am trying to read a non-text file as a string by using Python
    read(), however, it seems there is some thing wrong with it. I can use
    read() on text file correctly, but unable to read .xls file correctly.
    (The program can read any file correctly in Fedora 10)

    Any idea how to solve this problem?

    Thank you very much!
    Chances are you forgot the "b" parameter to open(). Unnecessary in
    Unix, it tells the library to *not* translate \r\n to \n upon read, or
    the inverse on write. In other words, with the "b" parameter, the file
    is read in unchanged.

    infile = open(filename, "rb")
    ....
  • Li Wang at May 8, 2009 at 9:08 am
    Hi Dave:
    Thank you very much for you explanation:)

    Chances are you forgot the "b" parameter to open(). Unnecessary in Unix, it
    tells the library to *not* translate \r\n to \n upon read, or the inverse
    on write. In other words, with the "b" parameter, the file is read in
    unchanged.
    So, if I am using python in Linux, do open('file', 'r') and
    open('file', 'rb') work the same way?

    Thanks,

    Best regards,
    Li
  • Li Wang at May 9, 2009 at 2:59 am

    (my smtp mail server has been down for a few hours, so you have some other
    responses.)

    yes, as far as I can tell from the docs, the 'b' flag doesn't matter in
    Unix/Linux. Note that I haven't actually been on a Unix machine since
    starting with Python, but it matches what else I know. And if it's
    different in Python 3.0, I wouldn't know that either.,
    Thank you very much!


    --
    Li
    ------
    Time is all we have
    and you may find one day
    you have less than you think
  • Pruebauno at May 8, 2009 at 1:20 pm

    On May 8, 5:08?am, Li Wang wrote:
    Hi ?Dave:
    Thank you very much for you explanation:)
    Chances are you forgot the "b" parameter to open(). ?Unnecessary in Unix, it
    tells the library to *not* translate \r\n ?to \n upon read, or the inverse
    on write. ?In other words, with the "b" parameter, the file is read in
    unchanged.
    So, if I am using python in Linux, do open('file', 'r') and
    open('file', 'rb') work the same way?

    Thanks,

    Best regards,
    Li
    In old Python up to 2.6, YES.
  • Li Wang at May 8, 2009 at 1:47 pm

    2009/5/8 <pruebauno at latinmail.com>:
    On May 8, 5:08 am, Li Wang wrote:
    Hi Dave:
    Thank you very much for you explanation:)
    Chances are you forgot the "b" parameter to open(). Unnecessary in Unix, it
    tells the library to *not* translate \r\n to \n upon read, or the inverse
    on write. In other words, with the "b" parameter, the file is read in
    unchanged.
    So, if I am using python in Linux, do open('file', 'r') and
    open('file', 'rb') work the same way?

    Thanks,

    Best regards,
    Li
    In old Python up to 2.6, YES.
    Thank you:D



    --
    Li
    ------
    Time is all we have
    and you may find one day
    you have less than you think
  • Scott David Daniels at May 8, 2009 at 1:32 pm

    Li Wang wrote:
    So, if I am using python in Linux, do open('file', 'r') and
    open('file', 'rb') work the same way?
    You get identical results, but you ar lying to the reader of your code.
    you should include the 'b' if what you want is bytes (or octets if you
    prefer), and not use it if what you expect is "text." Your code
    becomes less confusing by using 'b' properly, even if you see no
    particular difference.

    --Scott David Daniels
    Scott.Daniels at Acm.Org
  • Li Wang at May 8, 2009 at 1:47 pm

    2009/5/8 Scott David Daniels <Scott.Daniels at acm.org>:
    Li Wang wrote:
    So, if I am using python in Linux, do open('file', 'r') and
    open('file', 'rb') work the same way?
    You get identical results, but you ar lying to the reader of your code.
    you should include the 'b' if what you want is bytes (or octets if you
    prefer), and not use it if what you expect is "text." Your code
    becomes less confusing by using 'b' properly, even if you see no
    particular difference.
    Thanks a lot, very helpful:D
    >



    --
    Li
    ------
    Time is all we have
    and you may find one day
    you have less than you think

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedMay 8, '09 at 5:04a
activeMay 9, '09 at 2:59a
posts11
users5
websitepython.org

People

Translate

site design / logo © 2022 Grokbase