#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0

    Writing to binary file in Python 3?


    So, I've got a list of string hex values, ie ["FC", "05"] etc. In python 2, to write to a binary file (opened in "rb+"), writing would be as simple as doing file.write(string[index]).

    But in python 3, it's a bit different. Since bytes and strings are separate, I now have:

    file.write(string[index].encode()). However, that would put the literal values into the file, not the hex values. For example, if I did

    Code:
    file.write("00".encode())
    Then the hex in the file will be "31" (which is the literal hex representation of 0 in ASCII), instead of the expected value of "00". How can I actually write the hex values into the file?
  2. #2
  3. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    You could use module pickle (works with Python27 and Python33) ...
    Code:
    import pickle
    
    fname = "hexfile.dat"
    
    x = "F5"
    with open(fname, 'wb') as fout:
        pickle.dump(x, fout)
    
    with open(fname, 'rb') as fin:
        x2 = pickle.load(fin)
    
    print(x2, type(x2), int(x2, 16))  # F5 <class 'str'> 245
    Note: your hex number will be a string type in Python2 and Python3
    Last edited by Dietrich; October 22nd, 2013 at 04:38 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  4. #3
  5. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    With Python2 your hex number will always be a string type, even if you use binary write and read modes ...
    Code:
    # Python27 only
    
    fname = "hexfile.txt"
    
    x = "F5"
    with open(fname, 'wb') as fout:
        fout.write(x)
    
    with open(fname, 'rb') as fin:
        x2 = fin.read()
    
    print(x2, type(x2), int(x2, 16))  # ('F5', <type 'str'>, 245)
    Last edited by Dietrich; October 22nd, 2013 at 04:36 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  6. #4
  7. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    To use binary read and write in Python3 you have to encode the string, works fine for me ...
    Code:
    # Python3 only
    
    fname = "hexfile.byte"
    
    # encode string to <class 'bytes'> or byte string
    s = "F5"
    x = s.encode()
    
    with open(fname, 'wb') as fout:
        fout.write(x)
    
    with open(fname, 'rb') as fin:
        x2 = fin.read()
    
    print(x2, type(x2), int(x2, 16))  # b'F5' <class 'bytes'> 245
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  8. #5
  9. Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2005
    Posts
    588
    Rep Power
    64
    Another option is to do a string write and a binary read (this way you can avoid encode() with Python3) ...
    Code:
    # Python3
    
    fname = "hexfile.txt"
    
    s = "F5"
    
    # string write
    with open(fname, 'w') as fout:
        fout.write(s)
                   
    # binary read
    with open(fname, 'rb') as fin:
        x2 = fin.read()
    
    print(x2, type(x2), int(x2, 16))  # b'F5' <class 'bytes'> 245
    Last edited by Dietrich; October 22nd, 2013 at 04:42 PM.
    Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2011
    Posts
    2
    Rep Power
    0
    Got it to work, thanks.

IMN logo majestic logo threadwatch logo seochat tools logo