简体   繁体   中英

How to convert Avro file to CSV file in python?

The process I am following is -

  1. Converting Avro to JSON
  2. Then converting JSON to CSV

Is there any direct way to convert the Avro file to CSV?

This is how I converted an avro file to 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM