S
S
shudry_toster2018-02-12 22:34:53
Django
shudry_toster, 2018-02-12 22:34:53

How to solve encoding problem?

Help with coding. There is some functionality, namely: from the client side, an .xls file comes through AJAX. There, using the function, pyexcel.get_array()an array is obtained and sent back to the client as a file. Here is the code:

import pyexcel as pe

file_exist = request.FILES.get('upload_excel_file', None)
records_array = pe.get_array(file_type='xls', file_content=file_exist.read(), start_row=8)

response = HttpResponse(records_array)
response['Content-Type'] = 'text/plain'
response['Content-Disposition'] = 'attachment; filename=response-text.txt'
return response

The file has something like this:
[u'\u0421\u043e\u0435\u0434\u0438\u043d\u0438\u0442\u0435\u043b\u044c \u043f\u0440\u043e\u0444\u0438\u043b\u0435\u0439 \u043a\u0440\u0430\u0431', '', '', 2195, '', 27216.15][u'\u0414\u044e\u0431\u0435\u043b\u044c-\u0433\u0432\u043e\u0437\u0434\u044c \u0433\u0440\u0438\u0431 "\u0423\u043a\u0440\u0430\u0438\u043d\u0430" 6\u044540(200\u0448\u0442)', '', '', 2000, '', 3560]

And if each element from the list is encoded.encode('utf-8')
['\xd0\xa1\xd0\xbe\xd0\xb5\xd0\xb4\xd0\xb8\xd0\xbd\xd0\xb8\xd1\x82\xd0\xb5\xd0\xbb\xd1\x8c \xd0\xbf\xd1\x80\xd0\xbe\xd1\x84\xd0\xb8\xd0\xbb\xd0\xb5\xd0\xb9 \xd0\xba\xd1\x80\xd0\xb0\xd0\xb1', '', '',

Interestingly, only Russian letters are not displayed, for example
\u041a\u0430\u0431\u0435\u043b\u044c (\u043f\u0440\u043e\u0432\u043e\u0434) FTP 4 Cat 5e 24 AWG Cu (SUPRLAN)'

And if you just pass a list of elements to the page and show (without setting response headers) everything works fine.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Web Dentist, 2018-02-13
@shudry_toster

Here

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question