简体   繁体   中英

Getting error " 'NoneType' object has no attribute 'getvalues' "

I have a code which reads a csv file and manufacture large data from it where i'm spliting the data on the basis of columns into 3 separate csv files and i need to zip these 3 files in a single file.

However i'm getting the following error :

'NoneType' object has no attribute '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

i'm unable to get the zip file please tell me where i'm doing wrong.

The conflict comes from keeping the same names for different objects. data1 , data2 , data3 should stay dataframes, this way is more clear for everyone.

file_List = [data1,data2,data3] is a list of None , because the method to_csv returns None if you pass a string (see to_csv documentation ). Just remove the argument when you call the method to get a result as a string.

You can remove every data**=data**.to_csv("parts.csv") and get the values directly where you need it (in zip creation)

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()

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