admin管理员组文章数量:1287593
def createlist(request):
if request.method == 'POST':
files = request.FILES['ListFile']
print(type(files))
csv_file = csv.DictReader(files)
for i in csv_file:
print(i)
return HttpResponse("ok")
This gives
class 'django.core.files.uploadedfile.InMemoryUploadedFile'
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
Here i post this file using ajax
js:
$('#form1').ajaxForm(function(data) {
alert(data) ;
});
def createlist(request):
if request.method == 'POST':
files = request.FILES['ListFile']
print(type(files))
csv_file = csv.DictReader(files)
for i in csv_file:
print(i)
return HttpResponse("ok")
This gives
class 'django.core.files.uploadedfile.InMemoryUploadedFile'
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
Here i post this file using ajax
js:
$('#form1').ajaxForm(function(data) {
alert(data) ;
});
Share
Improve this question
asked May 29, 2014 at 7:18
itzMEonTVitzMEonTV
20.4k4 gold badges44 silver badges51 bronze badges
5
-
What does
print([type(x) for x in files.readlines()])
give you? – dilbert Commented May 29, 2014 at 7:26 - @dilbert [<class 'bytes'>, <class 'bytes'>, – itzMEonTV Commented May 29, 2014 at 7:39
- DING, DING, DING, we have a winner!! – dilbert Commented May 29, 2014 at 7:42
- @dilbert any solution to read files as dictreader to get as dictionary – itzMEonTV Commented May 29, 2014 at 7:45
-
You need to either a) change
request.FILES['ListFile']
to a non-binary read mode or b) read all ofrequest.FILES['ListFile']
into an intermediate file-like object. I would remend a). – dilbert Commented May 29, 2014 at 7:47
2 Answers
Reset to default 14Using codec.iterdecode, i solved it.I think this is due to python 3.x
import codecs
def createlist(request):
if request.method == "POST":
fil = request.FILES['ListFile']
csvfile = csv.DictReader(codecs.iterdecode(fil, 'utf-8'))
for i in csv_file:
print(i)
return HttpResponse("ok")
The CSV doesn't support UTF8. It needs the file-like object to be encoded. Refer to here for more information.
import codecs
def createlist(request):
if request.method == "POST":
utf8_file = codecs.EncodedFile(request.FILES["ListFile"],"utf-8")
csv_file = csv.DictReader(utf8_file)
for i in csv_file:
print(i)
return HttpResponse("ok")
本文标签: javascriptdjango csv file upload managingStack Overflow
版权声明:本文标题:javascript - django csv file upload managing - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741265660a2368412.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论