Ruby中Python的encode()的等价物是什么

问题描述 投票:1回答:2

我正在将Python脚本转移到Ruby。

经常使用python方法'encode()'。例子:

apikey.encode()
encoded_payload = json.dumps(payload).encode()

看起来Ruby等价物有一些需要添加的必需参数。 UTF-8或ISO-8859-1。哪一个匹配标准的python编码()。

python ruby encode
2个回答
1
投票

红宝石:

   string = "some string".encode
   string.encoding

编码:UTF-8

所以Ruby的default是UTF-8

蟒蛇default也编码为UTF-8

所以没有要添加的参数,两个默认值都是相同的:UTF-8


为了完整性:

如果有人想要更改默认的UTF-8,你可以在python中看到它(参见documentarysupported encodings):

string.encode(encoding='UTF-8')

Ruby也一样:

string.encode("UTF-8")

支持Ruby encodings


0
投票

Python的str.encode()默认为utf-8,所以请继续使用。

© www.soinside.com 2019 - 2024. All rights reserved.