LoginSignup
9
8

More than 5 years have passed since last update.

Python でmessagepackを作ったり、読んだりする

Posted at
sh

   sudo yum install python-pip -y

   sudo pip install msgpack-python msgpack-tool

test.csv : 空白区切りのCSVファイル
test.pack : 浮動小数点型として作成されたpack作成

packer.py

import csv
import msgpack

infile = csv.reader(open('test.csv', 'rb'), delimiter=' ')
outfile = open('test.pack','w')
packer = msgpack.Packer()
for row in infile:
    outfile.write(packer.pack(map(float,row)))

標準入力でパイプしてpackファイルの中身をunpackしてみる

zcat test.pack.gz | unpacker.py

unpacker.py

import sys
import msgpack

BUF_SIZE = 64*1024
unpacker = msgpack.Unpacker()
while True:
    buf = sys.stdin.read(BUF_SIZE)
    if not buf:
        break
    unpacker.feed(buf)
    for obj in unpacker:
        print( obj )

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8