看板Python
標 題Re: [問題] 程式輸出 pipe redirect 與 Unicod …
發信站老鼠的香香乳酪洞 (Mon Sep 8 18:54:29 2008)
轉信站ptt!ctu-reader!ctu-peer!news.nctu!csnews.cs.nctu!news.cs.nctu!Mouse
※ 引述《[email protected] (靠北喔= = )》之銘言:
: 我有一個程式會 print 出中文, 如下
: bash-3.2$ ./test.py
: 油品行銷事業部台北市信義區松仁<b>路三號總機:(02)8789-8989 傳真:(02)8789</b>-9050 免費 服務專線:0800-036188 電子服務信箱:[email protected] <b>...</b>
: 地址: 11010台北市信義區松仁<b>路三號/ 總機: 02-8789-8989 / 傳真: 02-8789</b>-9000 加油 站客服電話: 0800-036188/網站瀏覽人數:3397635 人 <b>...</b>
: 但是當我想把輸出寫到一個檔案 或是pipe到more的時候
: bash-3.2$ ./test.py |more
: Traceback (most recent call last):
: ......
: UnicodeEncodeError:
: 'ascii' codec can't encode characters in position 0-14:
: ordinal not in range(128)
: 請問我做錯了什麼?
在 print 之前,先把字串用 utf-8 編碼
如:
print u"測試"
改成:
print u"測試".encode("utf-8")
--
※Post by zao from linus.csie.ntu.edu.tw
老鼠的香香乳酪洞˙電子佈告欄系統˙alexbbs.twbbs.org˙140.113.166.7