"Python2 UTF-8 한글 사용"의 두 판 사이의 차이

잔글 (로봇: 자동으로 텍스트 교체 (-<source lang='dos'> +<source lang='cli'>))
잔글 (로봇: 자동으로 텍스트 교체 (-[root@jmnote +[root@zetawiki))
7번째 줄: 7번째 줄:
*python 소스 코드에 UTF-8 한글이 포함될 경우 오류 발생
*python 소스 코드에 UTF-8 한글이 포함될 경우 오류 발생
<source lang='cli'>
<source lang='cli'>
[root@jmnote ~]# cat hello.py  
[root@zetawiki ~]# cat hello.py  
print "안녕"
print "안녕"
[root@jmnote ~]# python hello.py
[root@zetawiki ~]# python hello.py
   File "hello.py", line 1
   File "hello.py", line 1
SyntaxError: Non-ASCII character '\xec' in file hello.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
SyntaxError: Non-ASCII character '\xec' in file hello.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details
17번째 줄: 17번째 줄:
*파일 맨첫줄에 <code># -*- coding: utf-8 -*-</code> 추가
*파일 맨첫줄에 <code># -*- coding: utf-8 -*-</code> 추가
<source lang='cli'>
<source lang='cli'>
[root@jmnote ~]# cat hello2.py  
[root@zetawiki ~]# cat hello2.py  
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
print "안녕"
print "안녕"
[root@jmnote ~]# python hello2.py
[root@zetawiki ~]# python hello2.py
안녕
안녕
</source>
</source>

2015년 2월 12일 (목) 01:14 판

Python UTF-8 사용
Python 한글 사용
SyntaxError: Non-ASCII character '\xec'

1 문제상황

  • python 소스 코드에 UTF-8 한글이 포함될 경우 오류 발생
[root@zetawiki ~]# cat hello.py 
print "안녕"
[root@zetawiki ~]# python hello.py
  File "hello.py", line 1
SyntaxError: Non-ASCII character '\xec' in file hello.py on line 1, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

2 정상 예시

  • 파일 맨첫줄에 # -*- coding: utf-8 -*- 추가
[root@zetawiki ~]# cat hello2.py 
# -*- coding: utf-8 -*-
print "안녕"
[root@zetawiki ~]# python hello2.py
안녕

3 같이 보기