作者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
1F:→ alex0914:储存的编码是unicode吗@@? 02/14 22:25
2F:→ kurtping:是utf-8 02/14 22:44
3F:→ kurtping:已经找到解答了 原来是因为mac的文字编辑 没法开启 02/14 22:44
4F:→ kurtping:没有bom的档案 用textwrangler开的话就可以 自问自答了哈 02/14 22:45