python - Convert string to correct charset -


im trying save unicode data external webservice.

when try save æ-ø-å, saved æ-ø-Ã¥ in external system.

edit:

(my firstname value jørn) (value django j\\xf8rn)

firstname.value=user_firstname = jørn

here result if try use encode:

firstname.value=user_firstname.encode('ascii', 'replace') = j?rn

firstname.value=user_firstname.encode('ascii', 'xmlcharrefreplace') = jørn

firstname.value=user_firstname.encode('ascii', 'backslashreplace') = j\xf8rn

firstname.value=user_firstname.encode('ascii', 'ignore') = unicode error using ignore.

my form updating user:

def show_userform(request):     if request.method == 'post':          form = userform(request.post, request.user)           if form.is_valid():             u = userprofile.objects.get(username = request.user)               firstname = form.cleaned_data['first_name']             lastname = form.cleaned_data['last_name']              tasks.update_webservice.delay(user_firstname=firstname, user_lastname=lastname)               return httpresponseredirect('/thank-you/')      else:         form = userform(instance=request.user) # unbound form      return render(request, 'myapp/form.html', {         'form': form,     }) 

here task:

from suds.client import client  @task() def update_webservice(user_firstname, user_lastname):      membermap = client.factory.create('ns2:map')      firstname = client.factory.create('ns2:mapitem')     firstname.key="firstname"     firstname.value=user_firstname      lastname = client.factory.create('ns2:mapitem')     lastname.key="lastname"     lastname.value=user_lastname       membermap.item.append(firstname)     membermap.item.append(lastname)       d = dict(customerid='xxx', password='xxx', personid='xxx', contactdata=membermap)       try:         #send updates setperson function         result = client.service.setperson(**d)     except webfault, e:         print e   

what need do, make data saved correctly?

your external system interpreting utf-8 if latin-1, or maybe windows-1252. that's bad.

encoding or decoding ascii not going help. string not plain ascii.

if you're lucky, it's you're missing option in web service's api, tell you're sending utf-8.

if not, you've got quite maintenance headache on hands, can still fix back. web service took string encoded utf-8 , decoded latin-1, need exact reverse of that:

user_firstname = user_firstname.encode('latin-1').decode('utf-8') 

Comments