I am trying to use an HTTP Request to accept a CSV and then I am trying to get the data from each column in the CSV into a list. The problem is that whenever I try to get the data from the CSV it constantly returns as a blank list on my command line.
I have tried using IO:
csv_file = request.DATA
csv = csv_file.content.decode('utf-8')
data_set = csv.reader(io.StringIO(csv))
print data_set
However this results in an Attribute Error.
AttributeError: 'dict' object has no attribute 'content'
File "C:\Users\Varun\Documents\BitBucket\api\apicall\lib\site-packages\django\core\handlers\base.py", line 132, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\Varun\Documents\BitBucket\api\apicall\lib\site-packages\django\views\decorators\csrf.py", line 58, in wrapped_view
return view_func(*args, **kwargs)
File "C:\Users\Varun\Documents\BitBucket\api\apicall\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "C:\Users\Varun\Documents\BitBucket\api\apicall\lib\site-packages\rest_framework\views.py", line 407, in dispatch
response = self.handle_exception(exc)
File "C:\Users\Varun\Documents\BitBucket\api\apicall\lib\site-packages\rest_framework\views.py", line 404, in dispatch
response = handler(request, *args, **kwargs)
File "C:\Users\Varun\Documents\BitBucket\api\remodel\views.py", line 4922, in put
csv = csv_file.content.decode('utf-8')
class LeadCSVInvite(APIView):
permission_classes = (permissions.IsAuthenticated,)
parser_class = (FileUploadParser, MultiPartParser)
def put(self, request, filename, format=None):
csv_file = request.DATA
csv = csv_file.content.decode('utf-8')
print csv
## Check the messages.error
# if not csv_file.name.endswith('.csv'):
# return Response(status=400)
# #return Response({'errorCode': 1, 'errorMsg': "not csv"}, content_type= 'application/json', status=status.HTTP_400_BAD_REQUEST)
data_set = csv.reader(io.StringIO(csv))
print data_set
# print data_set
# io_string = io.StringIO(data_set)
# next(io_string)
emails = []
names = []
for column in data_set:
print column
first_name = column[0],
email = column[1]
emails.append(email)
names.append(first_name)
for i in range(len(emails)):
if emails[i] is None:
return Response(status=400)
#return Response({'errorCode': 1, 'errorMsg': "no email"}, content_type= 'application/json', status=status.HTTP_400_BAD_REQUEST)
print emails[i]
emails[i] = emails[i].lower().strip()
client_exists = User.objects.filter(email=emails[i]).exists()
if names[i] is None and not client_exists:
return Response(status=400)
#return Response({'errorCode': 2, 'errorMsg': "no first name"}, content_type = 'application/json', status=status.HTTP_400_BAD_REQUEST)
elif names[i] is not None:
client_name = names[i].strip()
print names[i]
I expect the names and emails list to be populated with the data from the first and second column of the CSV respectively.
The error itself shows that you are trying to call content attribute against a dict. Findout how csv file is passed from the frontend.