我有一个读取csv文件并从中制造大数据的代码,其中我将根据列将数据拆分为3个单独的csv文件,我需要将这3个文件压缩为一个文件。
但是我遇到以下错误:
“ NoneType”对象没有属性“ getvalues”
import pandas as pd
df=pd.read_csv("test.csv")
# do all operation here....
# ..........................
# at last come with Generated data and store in dataframe
result = pd.DataFrame(final_Array) # "final_Array" contains the data
data1=result.drop(result.iloc[:,64:], axis=1)
data1=data1.to_csv("parts.csv")
data2=result.drop(result.iloc[:,8:64], axis=1)
data2=data2.drop(data2.iloc[:,19:],axis=1)
data2 = data2.to_csv("Skills.csv")
data3=result.drop(result.iloc[:,8:75], axis=1)
data3 = data3.to_csv("predict.csv")
file_List =[data1,data2,data3]
def zipFiles(file_List):
outfile = io.BytesIO() # io.BytesIO() for python 3
with zipfile.ZipFile(outfile, 'w') as zf:
for n, f in enumerate(file_List):
zf.writestr("{}.csv".format(n), f.getvalues())
return outfile.getvalue()
zipped_file = zipFiles(file_List)
response = make_response(zipped_file)
response.headers["Content-Type"] = "application/octet-stream"
response.headers["Content-Disposition"] = "attachment; filename=my_file.zip"
return response
我无法获取zip文件,请告诉我我做错了什么地方。
答案 0 :(得分:1)
冲突源于为不同对象保留相同的名称。
data1
,data2
,data3
应该保留数据帧,这样对所有人来说都更加清晰。
file_List = [data1,data2,data3]
是None
的列表,因为如果传递字符串,方法to_csv
返回None
(请参见to_csv documentation)。只需在调用方法以字符串形式获取结果时删除参数。
您可以删除每个data**=data**.to_csv("parts.csv")
并直接在需要的地方获取值(在zip创建中)
data1=result.drop(result.iloc[:,64:], axis=1)
# data1=data1.to_csv("parts.csv")
data2=result.drop(result.iloc[:,8:64], axis=1)
data2=data2.drop(data2.iloc[:,19:],axis=1)
# data2 = data2.to_csv("Skills.csv")
data3=result.drop(result.iloc[:,8:75], axis=1)
# data3 = data3.to_csv("predict.csv")
file_List = [data1, data2, data3]
name_list = ['parts.csv', 'skills.csv','predict.csv']
def zipFiles(file_List):
outfile = io.BytesIO()
with zipfile.ZipFile(outfile, 'w') as zf:
for name, data in zip(name_list, file_List):
zf.writestr(name, data.to_csv())
return outfile.getvalue()