Differences between revisions 16 and 17
Revision 16 as of 2007-06-06 15:08:41
Size: 7598
Editor: ZoomQuiet
Comment:
Revision 17 as of 2007-06-29 15:38:50
Size: 9006
Editor: ZoomQuiet
Comment:
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
||'''status'''|| 草稿 || ZoomQuiet || 80% || ||'''status'''|| 完成 ;ZoomQuiet;95%,补充习题 ||
Line 76: Line 76:
def _smartcode(stream):
    try:
        ustring = unicode(stream, 'gbk')
    except UnicodeDecodeError:
        try:
            ustring = unicode(stream, 'big5')
        except UnicodeDecodeError:
            try:
                ustring = unicode(stream, 'shift_jis')
            except UnicodeDecodeError:
                try:
                    ustring = unicode(stream, 'ascii')
                except:
                    return u"bad unicode encode!"
Line 80: Line 94:
def _smartcode(stream):
    tryuni = ("gbk"
            ,"gb2312"
            ,"gb18030"
            ,"big5"
            ,"shift_jis"
            ,"iso2022_kr"
            ,"iso2022_jp"
            ,"ascii")
    try:
        for type in tryuni:
            try:
                ustring = unicode("%s}"%type+stream, type)
                #try decode by list
            except:
                continue
                #break!continue try decode guess
    except:
        return u"bad unicode encode!"
Line 82: Line 115:
Line 87: Line 121:
{{{聪明编码函式
{{{#!python
import chardet
def _smartcode(stream):
    """smart recove stream into UTF-8
    """
    ustring = stream
    codedetect = chardet.detect(ustring)["encoding"]
    print codedetect
    try:
        print ustring
        ustring = unicode(ustring, codedetect)
        print ustring
        return "%s %s"%("",ustring.encode('utf8'))
    except:
        return u"bad unicode encode try!"
Line 125: Line 174:
 

status

完成 ;ZoomQuiet;95%,补充习题

TableOfContents

1. -1 PyDay 实用化,中文!

你能够碰到的问题,99%的情况下其它人已经遇到过了,所以,最佳的解决方式就是找到那段别人解决相似问题的代码!

1.1. 回顾需求

小白已经实现的需求已经到达这般了:

  1. 可以扫描光盘内容并存储为硬盘上的文本文件
    • 存储成*.cdc 的文本文件
    • 可以快速指定保存目录
    • 可以快速指定保存的文件名
  2. 可以根据储存到硬盘上的光盘信息进行搜索
    • 可以搜索指定目录中所有*.cdc文件
    • 可以指定关键字进行搜索
      • 列出所有含有关键字的信息行

1.1.1. 进一步

回想起来一直尝试搜索的都是E文关键字,中文的没有试过;

来几下! ... 呜乎矣哉,什么也查不出来!

1.2. 查阅记录文本

attachment:badcdc-chinese.png

这种数据对嘛?

  • 当初为了简单使用文档中的基本型:{{{#'cdctools.py' 中 cdWalker(cdrom,cdcfile) 的动作

...

  • for root, dirs, files in os.walk(cdrom):
    • export+="\n %s;%s;%s" % (root,dirs,files)

... }}}就是使用 os.walk() 的天然输出组织成每一行:

/media/cdrom0/EVA/Death-Rebirth;[];['eva8-01.Mp3', 'eva8-02.Mp3',...]
    ^                          ^ ^  ^ 
    |                          | |  +- files列表,此目录的文件名 
    |                          | +- 各个数据段使用";" 分隔
    |                          +- dirs列表,子目录名,如果没有就为空
    +- 当前目录
  • 瞧着格式象,为什么到中文的地方就是问号呢?

1.3. 中文!永远的痛

不问不知道,一把辛酸泪哪...

  • 列表什么的一搜索才知道,只要是个中国人,不论整什么开发,中文!永远有问题的
  • 幸好比小白勤劳的人海了去,有关中文的Python 处理也是一搜一大堆
  • 但是!有时候,选择太多也是个问题;-)

1.3.1. 编码问题

attachment:coding.png

attachment:ipy-try-walk.png

  • 嗯嗯嗯,看着就不同,根据理解继续尝试是否理解

attachment:ipy-try-trans-utf8.png {{{ unicode(原始文本, 'utf8' ).encode('utf8') 文本 ==decode()--> [unicode] ==>encode()--> utf-8文本

  • ^ | | | | +- 最终的渴求 | | | +- 是为编码过程;可以从unicode 输出为任意编码 | | +- Python 内置支持的unicode 格式数据 | +- 是为解码过程,将已知编码的文本编译成宇宙通用的unicode数据 +- 原始文本信息,是什么编码你得知道!

}}}

  • 也就是说文件没有编码之说,大家其实都是二进制格式保存在硬盘中的,仅仅是在写入读取时需要使用对应的编码进行处理,以便操作系统配合相关软件/字体绘制到屏幕中给人看
    • 所以,关键问题是得知道原先这些字串数据是使用什么编码来编译的!
    • 但是,在Unicode 之前,都是使用类似对照表的形式来组织编码的,无法从串数据流本身中统一解出不同的文字来,
    • 只有猜!

ObpLovelyPython/CDay-1 (last edited 2012-03-03 10:28:10 by ZoomQuiet)