繁体   English   中英

使用重音符号将utf-8编码为base64

[英]Encoding utf-8 to base64 with accents

我有一些像这样的数据:

data1 = ['Agos', '30490349304']
data2 = ['Desir\xc3\xa9','9839483948']

我使用的API期望数据以base64编码,所以我要做的是:

data = data1
string = base64.b64encode("Hi, %s! Your code is %s" % (data[0], data[0]))
myXMLRPCCall(string)

与data1一起正常工作。 使用data2时,编码可以正常进行,但是XMLRPC返回错误,因为它(从API文档中)期望仅使用ISO-8859-1(Latin1)字符。
我的问题是:如何将我的字符串转换为Latin1以便API接受它?

首先,确保您对编码等不感到困惑。例如,阅读this

然后注意,主要问题不在于base64编码,而是在于您试图将字节字符串(Python 2.x中的普通字符串)放入Unicode字符串。 我相信您可以通过从示例代码中的最后一个字符串中删除“ u”来解决此问题。

base64.b64encode("Hi, %s! Your code is %s" % (data[0].decode('utf8').encode('latin1'), data[0]))

这似乎起作用:

...

data = data2
base64.b64encode("Hi, %s! Your code is %s" % (data[0], data[0]))
# => 'SGksIERlc2lyw6khIFlvdXIgY29kZSBpcyBEZXNpcsOp'

# I can't test the XMLRPC parts, so this is just a hint ..
for_the_wire = base64.b64encode("Hi, %s! Your code is %s" % (data[0], data[0]))
latin_1_encoded = for_the_wire.encode('latin-1')

# send latin_1_encoded over the wire ..

一些python(2.X)unicode读数:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM