作者kurtping (木棍)
看板Python
標題[問題] Python新手 中文編碼問題
時間Tue Feb 14 21:35:55 2012
各位前輩好 我的環境是Mac 使用Textwrangler Python是2.6.1
問題是這樣的
#-*- coding: utf-8 -*-
file = open('test.txt', 'w')
str = 'hi how are you '
str = str.decode('utf-8')
str = str.encode('utf-8')
file.write(str)
file.closed
在這個情況下 輸出的text.txt 可以正常顯示
但是只要str = 'hi how are you 嗨你好'
有中文字出現在其中 輸出的檔案Mac系統就會說有未知格式不能開啟
摸索了好久 也爬文跟Google
但好像都沒找到我可以學習的方法
把decode encode那個拿掉也沒啥改變
摸了一整天摸不出頭緒 可以請各位前輩指點一下新手嗎
先說謝謝了
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.171.225.165
→ alex0914:儲存的編碼是unicode嗎@@? 02/14 22:25
→ kurtping:是utf-8 02/14 22:44
→ kurtping:已經找到解答了 原來是因為mac的文字編輯 沒法開啟 02/14 22:44
→ kurtping:沒有bom的檔案 用textwrangler開的話就可以 自問自答了哈 02/14 22:45