我正在尝试使用python 3.5在Ubuntu上运行一个非常简单的python脚本,但看到一个错误。相同的示例适用于python2.7
with open("sensor reading {}.txt".format(datetime.now().strftime('%d-%m-%Y-%H-%M')), "ab") as ff:
np.savetxt(ff, np.expand_dims(timestamped_camera_readings, axis=0),fmt='%f')
#!/usr/bin/env python
# -*- coding: utf-8 -*-
my_list = ['éléphant', 'Hello World']
print(my_list[0])
print(my_list[1])
Traceback (most recent call last):
File "test.py", line 5, in <module>
print(my_list[0])
UnicodeEncodeError: 'ascii' codec can't encode character '\xe9' in position 0: ordinal not in range(128)
如果我按以下方式更改上面的代码,则python3.5会按如下所示打印编码的字符
éléphant
Hello World
#!/usr/bin/env python
# -*- coding: utf-8 -*-
my_list = ['éléphant', 'Hello World']
print(my_list[0].encode("utf-8"))
print(my_list[1])
关于如何获取python3打印与python2.7类似的输出的任何想法吗?