Python Forum
How do I write a single 8-bit byte to a file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How do I write a single 8-bit byte to a file?
#1
Here's testfile.csv, where I'm storing values for my script to interact with.
testfile.bin,0,0102030405
testfile.bin,16,060708090A
testfile.bin,32,0B0C0D0E0F
First value is the file to write to, second value is the address to seek to, third value is a set of concatenated 8-bit bytes to write in that exact order to whatever is at the addresses at and after the seek address.

Note that the third value can and will vary in length when in production, but will always be made up of some number of 8-bit bytes.

It needs to be this way because I'm using spreadsheets to create simple editors for data tables in a video game, and exporting one sheet where all this stuff is concatenated to a CSV which my script will write to the files.

Here's a screencap of testfile.bin open in a hex editor, which is 0x2F unsigned 8-bit bytes long.
Before I run the script:
https://files.catbox.moe/pgba8l.png
After I run the script:
https://files.catbox.moe/mi40u8.png

Here's the script:
import sys,io, struct

csvName = sys.argv[1]
with open(csvName, "r") as f:
    line = f.readline()
    while line != '':
        lineList = line.split(',')
        with open(lineList[0],"r+b") as writeFile:
            writeFile.seek(int(lineList[1]))
            cntr = 0
            nmbr = lineList[2][cntr:cntr+2]
            writeFile.write(bytes(nmbr,'utf-8'))
            #print(bytes(nmbr,'utf-8'))
            #print(nmbr)
            #print(bytes(nmbr))
            #need to step through above for string slicing
            #while cntr < lineList
            #next do a loop where you grab lineList[3][n:n+1],
            #convert it from a hex string to an int,
            #and write it.
            #maybe int(lineList[3][n:n+1],16)
            cntr += 1
        line = f.readline()
I can't use
writeFile.write(int(lineList[2][cntr:cntr+2],16))
because you can only write a bytes object (which is a less coherent way to say "an array of 8-bit bytes") with write().

But there's no way I can find to turn an int into a byte, or byte object, or whatever this is. The bytes() method, with only an int as a parameter, returns an array of length int filled with 0x00 bytes.

The bytes(string, "utf-8") apparently writes an array of C-style char for every character in the string, as you can see in the second screencap above.

I'm trying to develop this thing and test it incrementally, and nothing I develop incrementally works. Is there literally no way to turn my use case into something I can actually use in Python 3?

BTW, I really don't appreciate people telling me to "do some experiments, bruh" (what does it look like I've been doing?) or drive-by posting links to the documentation (which is here: https://docs.python.org/3/). This is a help forum, I wouldn't be posting here if I didn't need help, and if you can't help me or contribute something useful then just don't post. It's that simple.
Reply
#2
Those aren't ints. You describe the bit at the end as "concatenated 8-bit bytes", but I'm guessing they're actually hexadecimal strings, so each character represents a 4-bit value.

You can create a byte object directly from a hex string like this:

>>> nmbr = bytes.fromhex("0102030405")
>>> nmbr
b'\x01\x02\x03\x04\x05'
>>> len(nmbr)
5
Since you've opened the file as binary and you have a bytes object, you can write it directly. There's no encoding necessary here.
Reply
#3
Thanks, it works.

import sys,io

csvName = sys.argv[1]
with open(csvName, "r") as f:
    line = f.readline()
    while line != '':
        lineList = line.split(',')
        with open(lineList[0],"r+b") as writeFile:
            writeFile.seek(int(lineList[1]))
            writeFile.write(bytes.fromhex(lineList[2]))
        line = f.readline()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 404 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,500 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,427 Nov-09-2023, 10:56 AM
Last Post: mg24
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 1,319 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  How do I read and write a binary file in Python? blackears 6 6,496 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,088 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,584 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,894 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  python Multithreading on single file mg24 3 1,725 Nov-05-2022, 01:33 PM
Last Post: snippsat
  Create multiple/single csv file for each sql records mg24 6 1,384 Sep-29-2022, 08:06 AM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020