0

I have simple application written in django with one form that uploads CSVfile.

My CSV file has a lot of columns with cyrillic letters. Just for testing I made my view handle html form like this:

@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')

When I try to upload my CSV, I get this error:

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

First I thought that my CSV is corrupted. Then why other CSV readers and Excel can open it?

1 Answer 1

1

You may want to take a look at this answer "Line contains NULL byte" in CSV reader (Python)

which utilizes the codecs library:

import codecs
csvReader = csv.reader(codecs.open('file.csv', 'rU', 'utf-16'))

to open the file with different encodings (ex: utf-16)

Good luck :)

Sign up to request clarification or add additional context in comments.

2 Comments

I have made like this: csvReader = csv.reader(codecs.open(uploaded_file, 'rU', 'utf-16')) which throws me TypeError: file() argument 1 must be encoded string without null bytes, not str
In the linked answer, there is the second most voted answer, which gives another solution (manipulating your CSV). You may want to try it as well!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.