Python-在文件编写中处理非ascii字符

时间:2018-01-30 07:12:38

标签: python

当我运行此代码时:

category

我收到了错误:

  

SyntaxError :文件中的非ASCII字符'\ xe2',未声明编码

2 个答案:

答案 0 :(得分:1)

看起来您需要在脚本顶部设置编码以防止出现此错误。

<强>实施例

# -*- coding: utf-8 -*-

答案 1 :(得分:0)

As Rakesh pointed out, you can set an encoding cookie at the top of your script so that it handles non-ascii characters.

Alternatively, you could replace the double quotation marks around Ayodhya with ascii quotation marks:

str1= "Ram's birthplace is \"Ayodhya\" in Uttar Pradesh"