V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
wangleineo
V2EX  ›  问与答

怎样优雅解决 python 解析 xml gb2312 编码的问题?

  •  
  •   wangleineo · 2014-09-30 12:04:18 +08:00 · 20787 次点击
    这是一个创建于 3517 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用Python解析编码为gb2312的xml,
    <?xml version="1.0" encoding="gb2312" ?>
    <node>...</node>
    会报错:
    ValueError: multi-byte encodings are not supported
    试过ElementTree和minidom,都是如此。

    最后采用了先解码再编码为utf8:
    dstr = dstr.decode('gb2312').encode('utf-8'),
    然后再字符串替换
    dstr = dstr.replace('gb2312', 'utf-8')
    才可以解析xml。很不优雅!

    有什么好的解决方案?
    10 条回复    2014-10-01 19:07:18 +08:00
    icedx
        1
    icedx  
       2014-09-30 12:48:46 +08:00
    sudo apt-get install build-essential
    sudo apt-get install libsqlite3-dev
    sudo apt-get install sqlite3 # for the command-line client
    sudo apt-get install bzip2 libbz2-dev
    wget http://www.python.org/ftp/python/3.3.5/Python-3.3.5.tar.xz
    tar xJf ./Python-3.3.5.tar.xz
    cd ./Python-3.3.5
    ./configure --prefix=/opt/python3.3
    make && sudo make install
    mkdir ~/bin
    ln -s /opt/python3.3/bin/python3.3 ~/bin/py
    echo 'alias py="/opt/python3.3/bin/python3.3"' >> .bashrc
    ZavierXu
        2
    ZavierXu  
       2014-09-30 13:04:02 +08:00
    @icedx Python3已经解决了中文字符编码问题了么? 冲着这个我也要升级到python3啊...
    Tink
        3
    Tink  
       2014-09-30 13:22:49 +08:00
    Python3 +1
    先decode再encode的方法实在是悲摧
    pi1ot
        4
    pi1ot  
       2014-09-30 13:31:41 +08:00
    优雅的定义是什么
    fxbird
        5
    fxbird  
       2014-09-30 14:17:37 +08:00
    xml本来就应该统一成utf-8的编码,用gb也太不专业了吧
    wangleineo
        6
    wangleineo  
    OP
       2014-09-30 14:28:20 +08:00
    @pi1ot 优雅就是不粗暴
    @fxbird 和历史遗迹打交道 没办法
    fghzpqm
        7
    fghzpqm  
       2014-09-30 15:18:33 +08:00
    所有 Python 问题都可以用 «换 Python 3» 解决?

    $ ipython
    Python 3.4.1 (default, Aug 20 2014, 12:12:32)
    Type "copyright", "credits" or "license" for more information.

    In [1]: import xml.etree.ElementTree as ET

    In [2]: ET.parse('test.xml')
    ---------------------------------------------------------------------------
    ValueError Traceback (most recent call last)
    <ipython-input-2-df5d74dfe4aa> in <module>()
    ----> 1 ET.parse('test.xml')

    /Users/fghzpqm/.pyenv/versions/3.4.1/lib/python3.4/xml/etree/ElementTree.py in parse(source, parser)
    1185 """
    1186 tree = ElementTree()
    -> 1187 tree.parse(source, parser)
    1188 return tree
    1189

    /Users/fghzpqm/.pyenv/versions/3.4.1/lib/python3.4/xml/etree/ElementTree.py in parse(self, source, parser)
    596 # It can be used to parse the whole source without feeding
    597 # it with chunks.
    --> 598 self._root = parser._parse_whole(source)
    599 return self._root
    600 while True:

    ValueError: multi-byte encodings are not supported
    fghzpqm
        8
    fghzpqm  
       2014-09-30 15:30:42 +08:00   ❤️ 1
    icedx
        9
    icedx  
       2014-09-30 23:09:34 +08:00 via Android
    @ZavierXu 大部分奇葩问题Py3都解决的很好
    ZavierXu
        10
    ZavierXu  
       2014-10-01 19:07:18 +08:00
    @icedx NICE, 现在的Python3是越来越有用的理由了, 把手头Python27的项目做完之后, 以后都常使用3了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   3959 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 45ms · UTC 05:09 · PVG 13:09 · LAX 22:09 · JFK 01:09
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.