我有python 3.6,并安装了avro 1.8.2 https://avro.apache.org/docs/1.8.2/gettingstartedpython.html用于处理avro文件
pip install avro-python3
但是我找不到从现有词典列表中一次创建Avro文件的选项。
import avro.schema
from avro.datafile import DataFileWriter
from avro.io import DatumWriter
schema = avro.schema.Parse(open("user.avsc", "rb").read())
writer = DataFileWriter(open("users.avro", "wb"), DatumWriter(), schema)
writer.append({"name": "Alyssa", "favorite_number": 256})
writer.append({"name": "Ben", "favorite_number": 7, "favorite_color": "red"})
writer.close()
假设我已经有一个列表对象:
l = [
{u'favorite_color': None, u'favorite_number': 256, u'name': u'Alyssa'},
{u'favorite_color': u'red', u'favorite_number': 7, u'name': u'Ben'}
]
我不想在循环中一个接一个地添加
for i in l:
writer.append(i)
我想一口气做到这一点,因为 fastavro lib可以:
# Writing
with open('user.avro', 'wb') as out:
writer(out, parsed_schema, records)
请建议如何使用avro 1.8.2 lib完成它?