use*_*406 2 python unicode replace
尝试替换或删除此列表中的字符串以插入到不允许它们的数据库中
info=[[u'\xa0Buffalo\u2019s League of legends ...', '2012-09-05'], [u' \xa0RCKIN 0 - 1 WITHACK.nq\xa0 ', u'\xa0Buffalo\u2019s League of legends ...', '2012-09-05']]
Run Code Online (Sandbox Code Playgroud)
我用了这个代码
info = [[x.replace(u'\xa0', u'') for x in l] for l in info]
info = [[y.replace('\u2019s', '') for y in o] for o in info]
Run Code Online (Sandbox Code Playgroud)
第一行有效,但第二行无效,有什么建议吗?
删除第二行并执行以下操作:
\n\ninfo = [[x.encode('ascii', 'ignore') for x in l] for l in info]\nRun Code Online (Sandbox Code Playgroud)\n\n并看看结果是否可以接受。这将尝试将所有 unicode 转换为 ascii 并删除任何无法转换的字符。您只是想确保如果您丢失了重要的 unicode 字符,这不是问题。
\n\n>>> info=[[u'\\xa0Buffalo\\u2019s League of legends ...', '2012-09-05'], [u' \\xa0RCKIN 0 - 1 WITHACK.nq\\xa0 ', u'\\xa0Buffalo\\u2019s League of legends ...', '2012-09-05']]\n>>> info = [[x.encode('ascii', 'ignore') for x in l] for l in info]\n>>> info\n[['Buffalos League of legends ...', '2012-09-05'], [' RCKIN 0 - 1 WITHACK.nq ', 'Buffalos League of legends ...', '2012-09-05']]\nRun Code Online (Sandbox Code Playgroud)\n\n你的 Python 程序中有 Unicode 数据(这很好。)
\n\n>>> u = u'\\u2019'\nRun Code Online (Sandbox Code Playgroud)\n\n为了实现互操作性,最佳实践是将 Unicode 字符串写入utf-8. 这些是您应该存储在数据库中的字节:
>>> u.encode('utf-8')\n'\\xe2\\x80\\x99'\n>>> utf8 = u.encode('utf-8')\n>>> print utf8\n\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n\n然后,当您将这些字节读回到程序中时,您应该对它们进行解码:
\n\n>>> utf8.decode('utf8')\nu'\\u2019'\n>>> print utf8.decode('utf8')\n\xe2\x80\x99\nRun Code Online (Sandbox Code Playgroud)\n\n如果您的数据库无法处理,utf-8那么我会考虑获取一个新的数据库。
| 归档时间: |
|
| 查看次数: |
1738 次 |
| 最近记录: |