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
  'utf-8' codec can't decode byte 0xe2 in position 122031: invalid continuation byte tienttt 12 571 Sep-18-2020, 10:10 PM
Last Post: tienttt
  unable to write to log file Mekala 2 320 Aug-12-2020, 11:02 PM
Last Post: Mekala
  How to extract a single word from a text file buttercup 7 481 Jul-22-2020, 04:45 AM
Last Post: bowlofred
  List Won't Write in Text File IILawrenceII 4 398 Jul-17-2020, 11:16 PM
Last Post: j.crater
  Print output in single file using pramika loop deepakkhw 1 338 Jul-11-2020, 11:57 AM
Last Post: j.crater
  Process Data from one csv file and write to another CSV file specific column ajin9581 1 371 Jun-17-2020, 06:00 PM
Last Post: buran
  Write SQLite query result to file hjk6734 1 349 May-27-2020, 12:17 PM
Last Post: menator01
  Append JSON's and write to file faqsap 4 560 May-15-2020, 04:20 PM
Last Post: faqsap
  Print/write to file function tpolim008 4 546 Apr-01-2020, 07:59 PM
Last Post: tpolim008
  Write object to file medatib531 7 611 Apr-01-2020, 06:25 PM
Last Post: BrendanD

Forum Jump:

User Panel Messages

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