Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Revisions

1 of 3
hh54188
  • 15.7k
  • 35
  • 117
  • 194

Confuse about python unicode

I write a python file in windows7 by sublime text, there are some Chinese character in the file, so when I run it, the character become some unrecognizable code(the same in cmd and git bash):

# -*- coding: utf-8 -*- 
str = "测试"
print str
arr = []
arr.append(str)
print arr

the result is:

娴嬭瘯
['\xe6\xb5\x8b\xe8\xaf\x95']

How can I solve this problem? And what cause this problem?and the arr print result shouldn't be unicode like \uXXX?

BTW, without the # -*- coding: utf-8 -*- I can't even run it:

$ python test.py
 File "test.py", line 2
SyntaxError: Non-ASCII character '\xe6' in file test.py on line 2, but no encodi
ng declared; see http://www.python.org/peps/pep-0263.html for details

I just google the statement, why can't run without it?

hh54188
  • 15.7k
  • 35
  • 117
  • 194
lang-py

AltStyle によって変換されたページ (->オリジナル) /