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

25번째 줄: 25번째 줄:


==같이 보기==
==같이 보기==
*[[Python 주석]]
*[[리눅스 쉘 한글 깨짐]]
*[[리눅스 쉘 한글 깨짐]]
*[[django 설치]]
*[[django 설치]]
*[[Python 주석]]


[[분류: Python]]
[[분류: Python]]
[[분류: UTF-8]]
[[분류: UTF-8]]
[[분류: 한글]]
[[분류: 한글]]

2016년 8월 2일 (화) 10:30 판

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 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}