或者为pycharm设置代码模版,这样每次新建Python文件时会自动带上以上代码。如果不添加,即使中文字符串以u开头,也是编译不通过的。
如果不带u的字符在包含了# coding: utf-8的脚本中默认字符为UTF-8,一般也不会有什么问题。
UNICODE转GBK:# 带u的字符串为unicodes.encode('gbk')UNICODE转UTF-8# 带u的字符串为unicodes.encode('utf-8')Windows下的命令行参数为GBK编码,因此需要对字符串进行转换,转换方法有两种。
方法一:
# Create a new Unicode object from the given encoded string.# encoding defaults to the current default string encoding.# errors can be 'strict', 'replace' or 'ignore' and defaults to 'strict'.unicode(s, "gbk", "ignore")方法二:
s.decode('gbk', 'ignore')跨平台判断:
if sys.platform == 'win32': # win下命令行参数为gbk编码,转换字符 passelse: pass或:
s.decode('utf-8', 'ignore')新闻热点
疑难解答