我试图将数据写入文件中。
filename = "test.txt"
string = "Niñas and niños"
with open(filename, 'w') as element:
element.write(string)
这会返回以下错误:
"Traceback (most recent call last):
File "/Users/alex/Documents/Python/filewriter.py", line 5, in <module>
element.write(string)
UnicodeEncodeError: 'ascii' codec can't encode character '\xf1'
in position 2: ordinal not in range(128)"
我正在使用最新版本的Python、最新版本的MacOS以及SublimeText3作为我的编辑器。
有人知道发生了什么吗?