how to upload and read csv file in django using csv.DictReader?

后端 未结 3 1692
灰色年华
灰色年华 2020-12-16 20:56

I am reading csv file through upload and trying to store all values in a list

def upload(request):
    paramFile = request.FILES[\'file\'].read()
    data =          


        
3条回答
  •  鱼传尺愫
    2020-12-16 21:17

    in Python 3, to get the correct type (String not bytes) without reading the full file into memory you can use a generator to decode line by line:

    def decode_utf8(input_iterator):
        for l in input_iterator:
            yield l.decode('utf-8')
    
    def upload(request):
        reader = csv.DictReader(decode_utf8(request.FILES['file']))
        for row in reader:
            print(row)
    

提交回复
热议问题