TextMate在python中不执行encode()函数

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

朋友。

我处理TextMate中的一个问题,该问题通常用于编写代码。对于我的哈希映射脚本,我需要将字符串编码为字节。例如,如果我写:

password = "orange"
bytes_list = list(password.encode())
print(bytes_list)

[期望的输出是[111, 114, 97, 110, 103, 101],如果我从Python的IDLE运行脚本,即使我在shell中编写了相同的代码,我也会得到。但是,如果我在TextMate中运行代码,结果将产生['o', 'r', 'a', 'n', 'g', 'e'],因此很明显,TextMate中的encode()函数无法正常工作。有趣的是,如果我执行从TextMate到shell的代码,那么它们也不起作用。在TextMate的首选项中,编码设置为Unicode-UTF-8并安装了Unicode捆绑包。我找不到通过Google对此主题的任何答案。也许你们中有些人过去曾遇到过这样的问题。感谢您的帮助。

python encode textmate
1个回答
0
投票

您的TextMate使用的是Python 2.7:

$ python
Python 2.7.16 (default, Dec  3 2019, 07:02:07) 
[GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.37.14)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> password = "orange"
>>> bytes_list = list(password.encode())
>>> print(bytes_list)
['o', 'r', 'a', 'n', 'g', 'e']
>>>

Python 3将提供您期望的输出:

$ python3
Python 3.7.1 (v3.7.1:260ec2c36a, Oct 20 2018, 03:13:28) 
[Clang 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> password = "orange"
>>> bytes_list = list(password.encode())
>>> print(bytes_list)
[111, 114, 97, 110, 103, 101]
>>>
© www.soinside.com 2019 - 2024. All rights reserved.