我是python的新手,我正在尝试编写一个简单的代码,用于将文本文件转换为avro。我收到此错误,找不到模块。我可以在schema.py文件中清楚地看到解析模块存在。如果有人能帮我理解我可能做错了什么,我将不胜感激。
import avro.schema, csv, codecs
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter
def unicode_csv_reader(unicode_csv_data, dialect=csv.excel, **kwargs):
# csv.py doesn't do Unicode; encode temporarily as UTF-8:
csv_reader = csv.reader(utf_8_encoder(unicode_csv_data),
dialect=dialect, **kwargs)
for row in csv_reader:
# decode UTF-8 back to Unicode, cell by cell:
yield [unicode(cell, 'utf-8') for cell in row]
def utf_8_encoder(unicode_csv_data):
for line in unicode_csv_data:
yield line.encode('utf-8')
schema = avro.schema.parse(open('C:/test/test.avsc', "rb").read())
我在Windows 10上使用Python 3.5.2,avro-python3-1.8.1。
答案 0 :(得分:29)
您从tutorial获得了avro代码示例,但遗憾的是avro-python3
未对其进行更新。
而不是:
schema = avro.schema.parse(open('file.avsc', "rb").read())
您需要以文本模式阅读文件,并使用Parse()
方法:
schema = avro.schema.Parse(open('file.avsc', "r").read())