【发布时间】:2020-03-27 10:13:25
【问题描述】:
我正在开发一个新库,它允许用户将任何文件(xlsx、csv、json、tar、zip、txt)解析为生成器。
现在我被困在 zip 存档中,当我尝试从中解析 csv 时,我得到了
io.UnsupportedOperation: seek 紧跟在 elem.seek(0) 之后。 csv 文件是一个简单的 4x4 行和列。如果我使用csv_parser 解析csv,我得到了我想要的,但试图从一个zip 存档中解析它......繁荣。错误!
with open("/Users/ro/Downloads/archive_file/csv.zip", 'r') as my_file_file:
asd = parse_zip(my_file_file)
print asd
parse_zip 在哪里:
def parse_zip(element):
"""Function for manipulating zip files"""
try:
my_zip = zipfile.ZipFile(element, 'r')
except zipfile.BadZipfile:
raise err.NestedArchives(element)
else:
my_file = my_zip.open('corect_csv.csv')
# print my_file
my_mime = csv_tsv_parser.parse_csv_tsv(my_file)
print list(my_mime)
而parse_cvs_tsv 是:
def _csv_tsv_parser(element):
"""Helper function for csv and tsv files that return an generator"""
for row in element:
if any(s for s in row):
yield row
def parse_csv_tsv(elem):
"""Function for manipulating all the csv files"""
dialect = csv.Sniffer().sniff(elem.readline())
elem.seek(0)
data_file = csv.reader(elem, dialect)
read_data = _csv_tsv_parser(data_file)
yield '', read_data
我哪里错了?我打开文件的方式是好的还是......?
【问题讨论】:
标签: python python-2.7 csv zipfile