[Python] convert file to dict where value is a code, e.g. PovScore(1,2)

Discussão em 'Python' iniciado por Stack, Julho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I have following data (7MB) and it's stored in transpostion.txt:

    {'0x2df54c975eb48346': [PovScore(Cp(+57), BLACK), 5], ...}


    I did try

    with open("transposition.txt", "r") as file:
    x = file.readline()
    transposition = eval(x)
    del x


    but the memory reaches 99% and then hangs. I did try with 1MB file, it immediately loads and set the string, but when print it's very slow (IDLE):

    >>> with open("testfile.txt", "r") as file:
    x = file.read()

    >>> x
    Squeezed text (2000 lines)


    json module can't parse JSON with code:

    Traceback (most recent call last):
    File "<pyshell#19>", line 1, in <module>
    json.dumps({'a':povScore(0,0)})
    File "C:\Users\asdfs\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 231, in dumps
    return _default_encoder.encode(obj)
    File "C:\Users\asdfs\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
    File "C:\Users\asdfs\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
    File "C:\Users\asdfs\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 179, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
    TypeError: Object of type PovScore is not JSON serializable

    Continue reading...

Compartilhe esta Página