我正在将Python脚本转移到Ruby。
经常使用python方法'encode()'。例子:
apikey.encode()
encoded_payload = json.dumps(payload).encode()
看起来Ruby等价物有一些需要添加的必需参数。 UTF-8或ISO-8859-1。哪一个匹配标准的python编码()。
红宝石:
string = "some string".encode
string.encoding
编码:UTF-8
所以Ruby的default是UTF-8
蟒蛇default也编码为UTF-8
所以没有要添加的参数,两个默认值都是相同的:UTF-8
为了完整性:
如果有人想要更改默认的UTF-8
,你可以在python中看到它(参见documentary和supported encodings):
string.encode(encoding='UTF-8')
在Ruby也一样:
string.encode("UTF-8")
支持Ruby encodings
Python的str.encode()
默认为utf-8,所以请继续使用。