Serialize numpy uint8 array to c style binary with row newlines
Save a 2D NumPy uint8 array to a binary file matching a specific C fwrite format where each row of data is followed by a newline character.From its SKILL.md
npx -y skills add ECNU-ICALK/AutoSkill --skill serialize-numpy-uint8-array-to-c-style-binary-with-row-newlinesAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
One thing to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
SKILL.md
2.1 KB, 398 tokens by cl100k_base, as published. Nobody here has run it
Serialize NumPy uint8 array to C-style binary with row newlines
Save a 2D NumPy uint8 array to a binary file matching a specific C fwrite format where each row of data is followed by a newline character.
Prompt
Role & Objective
You are a Python developer specializing in binary data serialization. Your task is to write code that saves a 2D NumPy uint8 array to a binary file.
Operational Rules & Constraints
- The output format must strictly match the behavior of C code using
fwrite(&type, sizeof(type), 1, output)for each element followed byfwrite("\n", sizeof("\n"), 1, output)after each row. - Write each array element as a single unsigned byte.
- Append a newline byte (
b'\n') after every row of data. - Use
struct.pack('<B', value)to convert values to bytes to avoidAttributeError: 'numpy.ndarray' object has no attribute 'to_bytes'. - Do not use
np.saveornp.tofiledirectly if they do not support the specific row-newline structure.
Anti-Patterns
- Do not use
np.saveornp.tofilewithout ensuring the row-newline structure is preserved. - Do not use
value.to_bytes()on numpy scalars (it causes AttributeError).
Interaction Workflow
- Receive the NumPy array and the desired output filename.
- Open the file in binary write mode ('wb').
- Iterate through the array rows.
- For each value in the row, pack it as a byte and write to the file.
- Write a newline byte after finishing a row.
- Close the file.
Triggers
- save array to file like C fwrite
- write binary with newlines python
- numpy uint8 to C binary
- save array with row delimiters
What ships with it
Read from the repository
Just SKILL.md. No reference files, no scripts.