在Python中以二进制格式写入和读取Datetime [英] Write and read Datetime to binary format in Python

查看:124
本文介绍了在Python中以二进制格式写入和读取Datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将日期时间列表存储在Python的二进制文件中.

二进制"是指每种数据类型的最佳数字表示形式.这样做的应用是保存由(unix时间戳,纬度,经度,海拔)组成的GPS跟踪点,因此整个结构为小尾数"Long,float,float,float",每个值四个字节.

注意:由于对Unix平台没有任何影响,因此我不使用"unix-timestamp",而只是表示日期时间值的明确方式.

当前,我正在执行下面的代码,但是除了我仍在解决时区混乱(我的时区为-3)之外,我认为转换为int并返回不是正确的方法,因为如果我没记错的话,datetimedatetime64是python/numpy中的本机类型.因此,datetime64将需要八个字节,而不是我用于(长)unix时间戳的四个字节.

import datetime
import calendar
import struct

now = datetime.datetime.now()
print now

stamp = calendar.timegm(now.utctimetuple())
print stamp

binarydatetime = struct.pack('<L', stamp)
recoverstamp = struct.unpack('<L', binarydatetime)[0]
print recoverstamp

recovernow = datetime.datetime.fromtimestamp(recoverstamp)
print recovernow

所以主要问题是:这是将朴素的日期时间转换为二进制然后返回的有效方式吗?"

另外一个问题是:如果这段代码中的所有内容都是幼稚的,为什么会有时区偏移?"

感谢阅读!

解决方案

我找到了一种使用Unix时间戳并将其存储为整数的方法.这对我有用,因为我不需要亚秒级的分辨率,但是我认为长整数将允许微秒级的分辨率,并对代码进行一些修改.

与原始版本相比,更改之处在于将calendar.timegm替换为time.mktime,还用utctimetuple替换了timetuple,以保持所有内容幼稚.

此:

import datetime
import struct
import time

now = datetime.datetime.now()
print now

stamp = time.mktime(now.timetuple())
print stamp

recoverstamp = datetime.datetime.fromtimestamp(stamp)
print recoverstamp

binarydatetime = struct.pack('<L', stamp)
recoverbinstamp = struct.unpack('<L', binarydatetime)[0]
print recoverbinstamp

recovernow = datetime.datetime.fromtimestamp(recoverbinstamp)
print recovernow

返回此:

2013-09-02 11:06:28.064000
1378130788.0
2013-09-02 11:06:28
1378130788
2013-09-02 11:06:28

由此,我可以轻松地将打包的binarydatetime写入文件,并稍后再读回.

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 this is to save GPS trackpoints composed by (unix-timestamp, latitude, longitude, elevation), so the whole structure is little-endian "Long, float, float, float", with four bytes to each value.

NOTE: I don't use "unix-timestamp" due to any affection to the Unix platform, but only as an unequivocal way to represent the value of a datetime.

Currently, I am doing like the code below, but besides some timezone confusion that I'm still working out (my timezone is -3), I believe converting to int and back might not be the right way, since datetime and datetime64 are native types in python/numpy, if I'm not mistaken. Thus, a datetime64 would need eight bytes instead of the four I am using for the (long)unix-timestamp.

import datetime
import calendar
import struct

now = datetime.datetime.now()
print now

stamp = calendar.timegm(now.utctimetuple())
print stamp

binarydatetime = struct.pack('<L', stamp)
recoverstamp = struct.unpack('<L', binarydatetime)[0]
print recoverstamp

recovernow = datetime.datetime.fromtimestamp(recoverstamp)
print recovernow

So the main question is: "is this the pythonic way to converting naive datetime to binary and back?"

And the aditional question is: "if everything in this code is supposed to be naive, why do I have a timezone offset?"

Thanks for reading!

解决方案

I have found a way using the Unix timestamp and storing it as an integer. This works for me because I don't need a subsecond resolution, but I think long integers would allow for microsecond resolution with some modifications of the code.

The changes from my original consist in replacing calendar.timegm by time.mktime and also utctimetuple by timetuple, to keep everything naive.

This:

import datetime
import struct
import time

now = datetime.datetime.now()
print now

stamp = time.mktime(now.timetuple())
print stamp

recoverstamp = datetime.datetime.fromtimestamp(stamp)
print recoverstamp

binarydatetime = struct.pack('<L', stamp)
recoverbinstamp = struct.unpack('<L', binarydatetime)[0]
print recoverbinstamp

recovernow = datetime.datetime.fromtimestamp(recoverbinstamp)
print recovernow

Returns this:

2013-09-02 11:06:28.064000
1378130788.0
2013-09-02 11:06:28
1378130788
2013-09-02 11:06:28

From this, I can easily write the packed binarydatetime to file, and read it back later.

这篇关于在Python中以二进制格式写入和读取Datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆