使用Python计算AVRO文件中的行数

3

目前,我可以使用以下方法打开Avro文件:

import avro.schema
from avro.datafile import DataFileReader, DataFileWriter
from avro.io import DatumReader, DatumWriter

reader = DataFileReader(open("myfile.avro", "r"), DatumReader())
for user in reader:
  print (user)
reader.close()

在此过程之后,我希望能够使用Python代码计算这个文件中的行数。我目前正在使用Spyder中的Python 2.7。
感谢任何帮助。

1
您确定要计算行数吗?Avro文件是二进制的,在二进制文件中不存在“行”的概念。一种选择是将Avro文件转换为类似JSON的格式[1],然后计算记录(或行数,如果您确实需要这个)的数量。[1] https://github.com/linkedin/python-avro-json-serializer - Adrian Pop
1个回答

3

试试这个

num_lines = sum(1 for line in reader)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接