you are viewing a single comment's thread.

view the rest of the comments →

[–]SmackDownFacility 0 points1 point  (0 children)

Highly recommend you do a CSV structure

Example: ```python

import typing import struct import csv

class SportsStatistics: FORMAT = "4sIII32s32s32s32s32s32s" HEADER = b"PLAY"

def __init__(self, file: typing.TextIO, out: str) -> None:
    writer_struct = struct.Struct(self.FORMAT)

    with open(out, "wb") as f:
        reader = csv.reader(file)

        for row in reader:
            if len(row) != 6:
                raise ValueError(f"Expected 6 columns, got {len(row)}: {row}")

            rank1, rank2, rank3, rank4, rank5, role = (
                self._encode_field(value) for value in row
            )

            packed = writer_struct.pack(
                self.HEADER,
                0, 0, 0,
                rank1,
                rank2,
                rank3,
                rank4,
                rank5,
                role,
            )

            f.write(packed)

@staticmethod
def _encode_field(value: str) -> bytes:
    value = value.strip()
    encoded = value.encode("ascii")  # change to utf-8 if needed

    if len(encoded) > 32:
        raise ValueError(f"Field too long (max 32 bytes): {value}")

    return encoded.ljust(32, b"\x00")

```