栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

python编码utf-8

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python编码utf-8

您不需要对 已经 编码的数据进行编码。当您尝试执行此操作时,Python会先尝试对其进行 解码

unipre
然后再将其编码回UTF-8。这就是这里失败的原因:

    >>> data = u'u00c3' # Unipre data    >>> data = data.enpre('utf8')  # enpred to UTF-8    >>> data    'xc3x83'    >>> data.enpre('utf8')         # Try to *re*-enpre it    Traceback (most recent call last):      File "<stdin>", line 1, in <module>    UnipreDepreError: 'ascii' prec can't depre byte 0xc3 in position 0: ordinal not in range(128)

只需直接写您的数据文件,也 没有 必要编码已编码的数据。

如果改为建立

unipre
值,则实际上必须将那些值编码为可写入文件。您想使用
precs.open()
它,它返回一个文件对象,该文件对象将为您将Unipre值编码为UTF-8。

您也 确实 不想写UTF-8 BOM, 除非必须 支持否则无法读取UTF-8的Microsoft工具(例如MS Notepad)。

对于您的MySQL插入问题,您需要做两件事:

  • 添加

    charset='utf8'
    到您的
    MySQLdb.connect()
    通话中。

  • 使用

    unipre
    对象,而不是
    str
    查询或插入对象,而是 使用sql参数, 以便MySQL连接器可以为您做正确的事情:

    artiste = artiste.depre('utf8')  # it is already UTF8, depre to unipre

    c.execute(‘SELECt COUNT(id) AS nbr FROM artistes WHERe nom=%s’, (artiste,))

    c.execute(‘INSERT INTO artistes(nom,status,path) VALUES(%s, 99, %s)’, (artiste, artiste + u’/’))

如果您

precs.open()
改为自动解码内容,则实际上可能会更好:

    import precs    sql = mdb.connect('localhost','admin','ugo&(-@F','music_vibration', charset='utf8')    with precs.open('config/index/'+index, 'r', 'utf8') as findex:        for line in findex: if u'#artiste' not in line:     continue artiste=line.split(u'[:::]')[1].strip()        cursor = sql.cursor()        cursor.execute('SELECt COUNT(id) AS nbr FROM artistes WHERe nom=%s', (artiste,))        if not cursor.fetchone()[0]: cursor = sql.cursor() cursor.execute('INSERT INTO artistes(nom,status,path) VALUES(%s, 99, %s)', (artiste, artiste + u'/')) artists_inserted += 1

您可能需要复习Unipre和UTF-8和编码。我可以推荐以下文章:

  • 在Python的Unipre指南

  • Ned Batchelder的实用Unipre

  • 每个软件开发人员绝对,肯定必须绝对了解Unipre和字符集(无借口!)作者:Joel Spolsky



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617730.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号