下面的代码应该可以解决问题。首先,它打开一个文件并在lzma中对其进行解码,然后使用struct来解压缩二进制数据。
import lzmaimport structimport pandas as pddef bi5_to_df(filename, fmt): chunk_size = struct.calcsize(fmt) data = [] with lzma.open(filename) as f: while True: chunk = f.read(chunk_size) if chunk: data.append(struct.unpack(fmt, chunk)) else: break df = pd.Dataframe(data) return df
最重要的是要知道正确的格式。我四处搜寻,并尝试猜测和
'>3i2f'(或
>3I2f)效果很好。(这是3位整数的大int
2浮点数。您的建议:
'i4f'不会产生有意义的浮点数-
无论是大端还是小尾数。)有关
struct语法和格式的信息,请参阅docs。
df = bi5_to_df('13h_ticks.bi5', '>3i2f')df.head()Out[177]: 0 1 2 3 40 210 110218 110216 1.87 1.121 362 110219 110216 1.00 5.852 875 110220 110217 1.00 1.123 1408 110220 110218 1.50 1.004 1884 110221 110219 3.94 1.00更新资料
为了
bi5_to_df与https://github.com/ninety47/dukascopy的输出进行比较,我
test_read_bi5从那里编译并运行。输出的第一行是:
time, bid, bid_vol, ask, ask_vol2012-Dec-03 01:00:03.581000, 131.945, 1.5, 131.966, 1.52012-Dec-03 01:00:05.142000, 131.943, 1.5, 131.964, 1.52012-Dec-03 01:00:05.202000, 131.943, 1.5, 131.964, 2.252012-Dec-03 01:00:05.321000, 131.944, 1.5, 131.964, 1.52012-Dec-03 01:00:05.441000, 131.944, 1.5, 131.964, 1.5
并
bi5_to_df在同一输入文件上给出:
bi5_to_df('01h_ticks.bi5', '>3I2f').head()Out[295]: 0 1 2 3 40 3581 131966 131945 1.50 1.51 5142 131964 131943 1.50 1.52 5202 131964 131943 2.25 1.53 5321 131964 131944 1.50 1.54 5441 131964 131944 1.50 1.5因此,一切似乎都很好(ninety47的代码对列进行了重新排序)。
另外,使用
'>3I2f'代替
'>3i2f'(即
unsigned int代替
int)可能更准确。



