Write and read Datetime to binary format in Python

前端 未结 3 897
春和景丽
春和景丽 2021-01-03 06:10

I want to store a list of datetimes in a binary file in Python.

EDIT: by \"binary\" I mean the best digital representation for each datatype. The application for thi

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-03 07:00

    By far the most simple solution is to use Temporenc: http://temporenc.readthedocs.org/

    It takes care of all the encoding/decoding and allows you to write a Python datetime object to a file:

    now = datetime.datetime.now()
    temporenc.pack(fp, now)
    

    To read it back, this suffices:

    dt = temporenc.unpack(fp)
    print(dt)
    

提交回复
热议问题