【问题标题】:How to read a CSV from an HTTP Request using Django如何使用 Django 从 HTTP 请求中读取 CSV
【发布时间】:2019-08-02 17:23:05
【问题描述】:

我正在尝试使用 HTTP 请求来接受 CSV,然后尝试将 CSV 中每一列的数据获取到列表中。问题是,每当我尝试从 CSV 中获取数据时,它都会在我的命令行中不断返回为空白列表。

我尝试过使用 IO:

csv_file = request.DATA
csv = csv_file.content.decode('utf-8')        
data_set = csv.reader(io.StringIO(csv))
print data_set

但是这会导致属性错误。

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]

我希望姓名和电子邮件列表分别填充 CSV 第一列和第二列中的数据。

【问题讨论】:

标签: python-2.7 django-1.8


【解决方案1】:

错误本身表明您正在尝试针对 dict 调用 content 属性。了解如何从前端传递 csv 文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 2017-10-06
    • 1970-01-01
    • 2019-04-06
    • 1970-01-01
    • 2015-10-09
    相关资源
    最近更新 更多