我尝试使用msgpack将字典列表写入文件。但是,当我迭代Unpacker
的实例时,似乎数字10
在每个'真实'之间被解包。文档。
我正在运行的测试脚本是
import msgpack
from faker import Faker
import logging
from logging.handlers import RotatingFileHandler
fake = Faker()
fake.seed(0)
data_file = "my_log.log"
logger = logging.getLogger('my_logger')
logger.setLevel(logging.DEBUG)
handler = RotatingFileHandler(data_file, maxBytes=2000, backupCount=10)
logger.addHandler(handler)
fake_dicts = [{'name': fake.name()} for _ in range(100)]
for item in fake_dicts:
dump_string = msgpack.packb(item)
logger.debug(dump_string)
unpacker = msgpack.Unpacker(open(data_file))
for unpacked in unpacker:
print unpacked
我已使用fake-factory生成虚假数据。得到的打印输出如下:
{'name': 'Joshua Carter'}
10
{'name': 'David Williams'}
10
{'name': 'Joseph Jones'}
10
{'name': 'Gary Perry'}
10
{'name': 'Terry Wells'}
10
{'name': 'Vanessa Cooper'}
10
{'name': 'Michael Simmons'}
10
{'name': 'Nicholas Kline'}
10
{'name': 'Lori Bennett'}
10
我不明白为什么在每个字典之间打印数字10
?这是由logger
以某种方式介绍的吗?
答案 0 :(得分:2)
This is coming from the contents of unpacker. You can replicate yourself like this:
In [23]: unpacker = msgpack.Unpacker(open(data_file))
In [24]: unpacker.next()
Out[24]: {'name': 'Edward Ruiz'}
In [25]: unpacker.next()
Out[25]: 10