UnicodeDecodeError with Djangos request.FILES

2024/10/14 1:12:26

I have the following code in the view call..

def view(request):body = u""  for filename, f in request.FILES.items():body = body + 'Filename: ' + filename + '\n' + f.read() + '\n'

On some cases I get

UnicodeDecodeError: 'ascii' codec can't decode byte 0xf0 in position 7470: ordinal not in range(128)

What am I doing wrong? (I am using Django 1.1.)

Thank you.

Answer

Django has some utilities that handle this (smart_unicode, force_unicode, smart_str). Generally you just need smart_unicode.

from django.utils.encoding import smart_unicode
def view(request):body = u""  for filename, f in request.FILES.items():body = body + 'Filename: ' + filename + '\n' + smart_unicode(f.read()) + '\n'
https://en.xdnf.cn/q/69468.html

Related Q&A

bifurcation diagram with python

Im a beginner and I dont speak english very well so sorry about that. Id like to draw the bifurcation diagram of the sequence : x(n+1)=ux(n)(1-x(n)) with x(0)=0.7 and u between 0.7 and 4.I am supposed …

How do I use nordvpn servers as python requests proxies

Dont ask how, but I parsed the server endpoints of over 5000 nordvpn servers. They usually are something like ar15.nordvpn.com for example. Im trying to use nordvpn servers as request proxies. I know i…

Python Proxy Settings

I was using the wikipedia module in which you can get the information that is present about that topic on wikipedia. When I run the code it is unable to connect because of proxy. When I connected PC to…

Docker - Run Container from Inside Container

I have two applications:a Python console script that does a short(ish) task and exits a Flask "frontend" for starting the console app by passing it command line argumentsCurrently, the Flask …

Way to run Maven from Python script?

(I am using Windows.)I am trying to run maven from a python script. I have this:import subprocessmvn="C:\\_home\\apache-maven-2.2.1\\bin\\mvn.bat --version" p = subprocess.Popen(mvn, shell=Tr…

Why does testing `NaN == NaN` not work for dropping from a pandas dataFrame?

Please explain how NaNs are treated in pandas because the following logic seems "broken" to me, I tried various ways (shown below) to drop the empty values.My dataframe, which I load from a C…

Python dynamic import methods from file [duplicate]

This question already has answers here:How can I import a module dynamically given its name as string?(10 answers)How can I import a module dynamically given the full path?(37 answers)Closed last yea…

Python list does not shuffle in a loop

Im trying to create an randomized list of keys by iterating:import randomkeys = [1, 2, 3, 4, 5] random.shuffle(keys) print keysThis works perfect. However, if I put it in a loop and capture the output:…

Python extract max value from nested dictionary

I have a nested dictionary of the form:{2015-01-01: {time: 8, capacity: 5}, 2015-01-02: {time: 8, capacity: 7},2015-01-03: {time: 8, capacity: 8} etc}The dictionary is created from a csv file using dic…

Exponential Decay on Python Pandas DataFrame

Im trying to efficiently compute a running sum, with exponential decay, of each column of a Pandas DataFrame. The DataFrame contains a daily score for each country in the world. The DataFrame looks lik…