Processing a Django UploadedFile as UTF-8 with universal newlines

前端 未结 3 1820
梦谈多话
梦谈多话 2020-12-31 08:51

In my django application, I provide a form that allows users to upload a file. The file can be in a variety of formats (Excel, CSV), come from a variety of platforms (Mac, L

3条回答
  •  青春惊慌失措
    2020-12-31 09:28

    I use the csv.DictReader and it appears to be working well. I attached my code snippet, but it is basically the same as another answer here.

    import csv as csv_mod
    import codecs
    
    file = request.FILES['file']    
    dialect = csv_mod.Sniffer().sniff(codecs.EncodedFile(file,"utf-8").read(1024))
    file.open() 
    csv = csv_mod.DictReader( codecs.EncodedFile(file,"utf-8"), dialect=dialect )
    

提交回复
热议问题