module 'avro.schema' has no attribute 'parse'

Kevin K picture Kevin K · Dec 31, 2016 · Viewed 7.1k times · Source

i am new to python and i was trying to write a simple code for converting a text file to avro. i am getting this error that module not found. I could clearly see in the schema.py file that the parse module exists. I will appreciate if someone could help me understand what i may be doing wrong.

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())

I am using Python 3.5.2, avro-python3-1.8.1 on Windows 10.

Answer

jpdaigle picture jpdaigle · Feb 20, 2017

You got the avro code sample from their tutorial, but unfortunately it's not updated for avro-python3.

Instead of:

schema = avro.schema.parse(open('file.avsc', "rb").read())

You need to read the file in text mode, and use the Parse() method:

schema = avro.schema.Parse(open('file.avsc', "r").read())