Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串转换为正确的字符集_Python_Django_Soap_Suds_Python Unicode - Fatal编程技术网

Python 将字符串转换为正确的字符集

Python 将字符串转换为正确的字符集,python,django,soap,suds,python-unicode,Python,Django,Soap,Suds,Python Unicode,我正在尝试将unicode数据保存到外部Web服务 当我试图保存æ-å时,它会被保存为外部系统中的Ã-¸-Ã¥ 编辑: (我的名字值是Jørn)(值来自djangoJ\\xf8rn) firstname.value=user_firstname=Jørn 如果我尝试使用encode,以下是我的结果: firstname.value=user\u firstname.encode('ascii','replace')=J?rn firstname.value=user\u firstname.e

我正在尝试将unicode数据保存到外部Web服务

当我试图保存
æ-å
时,它会被保存为外部系统中的
Ã-¸-Ã¥

编辑: (我的名字值是
Jørn
)(值来自django
J\\xf8rn

firstname.value=user_firstname
=
Jørn

如果我尝试使用encode,以下是我的结果:

firstname.value=user\u firstname.encode('ascii','replace')
=
J?rn

firstname.value=user\u firstname.encode('ascii','xmlcharrefreplace')
=
Jø;rn

firstname.value=user\u firstname.encode('ascii','backslashreplace')
=
J\xf8rn

firstname.value=user\u firstname.encode('ascii','ignore')
=我使用ignore得到一个unicode错误

用于更新用户的我的表单:

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) # An unbound form

    return render(request, 'myapp/form.html', {
        'form': form,
    })
我的任务是:

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 to SetPerson function
        result = client.service.SetPerson(**d)
    except WebFault, e:
        print e  

要正确保存数据,我需要做什么?

使用
解码
编码
方法进行
str
键入。 例如:

x = "this is a test" # ascii encode 
x = x.encode("utf-8") # utf-8 encoded
x = x.decode("utf-8") # ascii encoded

您的外部系统将UTF-8解释为拉丁语-1,或者Windows-1252。那太糟糕了

编码或解码ASCII是没有帮助的。您的字符串绝对不是纯ASCII

如果幸运的话,只是在web服务的API中缺少了一些选项,通过这些选项,您可以告诉它您正在发送UTF-8

如果不是的话,你的手上有一个相当头疼的维护问题,但你仍然可以修复你得到的。web服务将您编码为UTF-8的字符串解码为Latin-1,因此您只需执行与此完全相反的操作:

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

什么是
user\u firstname
?user\u firstname来自我的userform当我尝试这样做时:
firstname.value=user\u firstname.decode('utf-8','ignore')
我得到这个错误:
unicodeincodeerror('ascii',u'\xe6-\xf8-\xe5-',6,7',ordinal不在范围内(128)
这个建议完全错误。您不能从ASCII“编码”到UTF-8,也不能从UTF-8“解码”到ASCII。您只能解码为Unicode并编码为ASCII或UTF-8(或其他编码)。我们可以从ASCII编码为UTF-8。因为ascii字符的UTF-8编码是相同的,我们没有数据丢失。但我不需要从ascii转换为UTF-8(我想)。似乎我需要将unicode字符(如
\\xf8
转换为
ø
\xf8不是unicode字符)。它是一个简单的字节。什么是原始数据的编码?当您使用decode方法时,它将数据转换为unicode,然后您可以在可能的情况下将unicode转换为其他格式。例如,无法将“\u0633”unicode字符转换为拉丁语-1,但可以将其转换为utf-8,其值为“\xd8\xb3”。