简体   繁体   English

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

[英]Encoding utf-8 to base64 with accents

I have some data like this: 我有一些像这样的数据:

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

I'm using an API that expects the data encoded in base64, so what I do is: 我使用的API期望数据以base64编码,所以我要做的是:

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

Which works fine with data1. 与data1一起正常工作。 With data2 the encoding goes ok, but then the XMLRPC returns an error, since it expects (from the API docs) only ISO-8859-1 (Latin1) characters. 使用data2时,编码可以正常进行,但是XMLRPC返回错误,因为它(从API文档中)期望仅使用ISO-8859-1(Latin1)字符。
My question is: how can I transform my string into Latin1 so that the API accepts it? 我的问题是:如何将我的字符串转换为Latin1以便API接受它?

First make sure you're not confused about encodings, etc. Read, for example, this . 首先,确保您对编码等不感到困惑。例如,阅读this

Then notice that the main problem isn't with the base64 encoding, but with the fact that you're trying to put byte string (normal string in Python 2.x) inside a Unicode string. 然后注意,主要问题不在于base64编码,而是在于您试图将字节字符串(Python 2.x中的普通字符串)放入Unicode字符串。 I believe you can fix this by removing the "u" from the last string in your example code. 我相信您可以通过从示例代码中的最后一个字符串中删除“ u”来解决此问题。

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

This seem to work: 这似乎起作用:

...

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 ..

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

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

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