如何在python中将Avro文件转换为CSV文件?

时间:2018-06-04 08:22:22

标签: python json python-3.x csv bigdata

我关注的过程是 -

  1. 将Avro转换为JSON
  2. 然后将JSON转换为CSV
  3. 有没有直接的方法将Avro文件转换为CSV?

1 个答案:

答案 0 :(得分:0)

这是我将avro文件转换为csv的方式:

    from fastavro import reader
import csv

head = True
count = 0
f = csv.writer(open("test.csv", "w+"))
with open('abc.avro', 'rb') as fo:
    avro_reader = reader(fo)
    for emp in avro_reader:
        #print(emp)
        if head == True:
            header = emp.keys()
            f.writerow(header)
            head = False
        count += 1
        f.writerow(emp.values())
print(count)