我有一个用django编写的简单应用程序,其中一个表单上传了CSV
文件。
我的CSV文件包含很多带西里尔字母的列。仅仅为了测试我使我的视图处理html表单如下:
@login_required()
@require_http_methods(["POST"])
def upload_csv(request):
uploaded_file = request.FILES['csv'].read(200)
data = csv.reader(uploaded_file)
for row in data:
print(row)
return redirect('moderator:upload_view')
当我尝试上传我的CSV时,出现此错误:
Traceback (most recent call last):
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 149, in get_response
response = self.process_exception_by_middleware(e, request)
File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 147, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "D:\Python27\lib\site-packages\django\contrib\auth\decorators.py", line 23, in _wrapped_view
return view_func(request, *args, **kwargs)
File "D:\Python27\lib\site-packages\django\views\decorators\http.py", line 42, in inner
return func(request, *args, **kwargs)
File "D:\myapp\moderator\views\upload.py", line 32, in upload_csv
for row in data:
Error: line contains NULL byte
[08/Apr/2017 13:53:41] "POST /moderator/upload_csv/ HTTP/1.1" 500 87052
首先我认为我的CSV已损坏。那么为什么其他CSV阅读器和Excel可以打开它?
答案 0 :(得分:1)
您可能需要查看此答案"Line contains NULL byte" in CSV reader (Python)
import codecs csvReader = csv.reader(codecs.open('file.csv', 'rU', 'utf-16'))
用不同的编码打开文件(例如:utf-16)
祝你好运:)