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
  Read and write active Excel file euras 4 359 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  Read file, reformat and write new file bryanmartin113 1 337 Jun-08-2021, 09:27 PM
Last Post: Larz60+
  Remove single and double quotes from a csv file in 3 to 4 column shantanu97 0 1,164 Mar-31-2021, 10:52 AM
Last Post: shantanu97
  write mariadb table rows query to each file? shams 1 573 Feb-02-2021, 04:10 PM
Last Post: buran
  Read/Write binary file deanhystad 3 726 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  Running A Parser In VSCode - And Write The Results Into A Csv-File apollo 5 990 Jan-14-2021, 08:58 PM
Last Post: snippsat
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 2,935 Sep-18-2020, 10:10 PM
Last Post: tienttt
  unable to write to log file Mekala 2 707 Aug-12-2020, 11:02 PM
Last Post: Mekala
  How to extract a single word from a text file buttercup 7 979 Jul-22-2020, 04:45 AM
Last Post: bowlofred
  List Won't Write in Text File IILawrenceII 4 757 Jul-17-2020, 11:16 PM
Last Post: j.crater

Forum Jump:

User Panel Messages

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