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
  how to write/overwrite data in a txt. file according to inp Quinn 2 1,543 Aug-12-2025, 04:20 PM
Last Post: Quinn
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 13 35,397 May-20-2025, 12:26 PM
Last Post: hanmen9527
  How to write variable in a python file then import it in another python file? tatahuft 4 2,201 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  [SOLVED] [Linux] Write file and change owner? Winfried 6 3,134 Oct-17-2024, 01:15 AM
Last Post: Winfried
  What does .flush do? How can I change this to write to the file? Pedroski55 3 2,196 Apr-22-2024, 01:15 PM
Last Post: snippsat
  Last record in file doesn't write to newline gonksoup 3 2,741 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 8,302 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 5,690 Nov-09-2023, 10:56 AM
Last Post: mg24
  Search for multiple unknown 3 (2) Byte combinations in a file. lastyle 7 4,414 Aug-14-2023, 02:28 AM
Last Post: deanhystad
  How do I read and write a binary file in Python? blackears 6 35,099 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.