某些 utf 字符不会显示在浏览器上并且 python 脚本失败
我从 Windows 7 上的 C# 应用程序生成了 SQL 脚本。名称条目具有 utf8 字符。它可以在 Windows 机器上找到,我使用 python 脚本来填充数据库。现在,相同的脚本在 Linux 平台上失败,抱怨这些特殊字符。
当我在 Windows 7 上生成包含 utf 字符的 XML 文件但无法在浏览器(IE、Firefox)上显示时,发生了类似的情况。
我曾经在 Windows XP 上生成这样的脚本,它在任何地方都能完美运行。
I generated a SQL script from a C# application on Windows 7. The name entries have utf8 characters. It works find on Windows machine where I use a python script to populate the db. Now the same script fails on Linux platform complaining about those special characters.
Similar things happened when I generated XML file containing utf chars on Windows 7 but fails to show up on browsers (IE, Firefox.).
I used to generate such scripts on Windows XP and it worked perfect everywhere.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请给出一个在“名称条目”中包含“utf8 字符”的脚本的小示例。您确定它们是 utf8 而不是像“cp1252”这样的 Windows 编码吗?是什么让你确定?在命令提示符下在 Python 中尝试一下:
输出中有趣的部分是它使用
\xhh
(其中 h 是任何十六进制数字)来表示非 ASCII 字符,例如\xc3\xa2
是带扬抑符的小 a 的 UTF-8 编码。向我们展示此类输出的代表性示例。另请告诉我们您从该示例脚本中获得的确切错误消息。更新:您似乎有以
cp1252
或类似形式编码的数据(Latin1
又名ISO-8859-1
是就像 Windows 上母鸡的牙齿一样罕见)。要使用 Python 将其转换为UTF-8
,您需要执行fixed_data = data.decode('cp1252').encode('utf8')
;我无法在 C# 方面为您提供帮助——您可能想就此提出一个单独的问题。Please give a small example of a script with "utf8 characters" in the "name entries". Are you sure that they are
utf8
and not some windows encoding like `cp1252'? What makes you sure? Try this in Python at the command prompt:The interesting parts of the output are where it uses
\xhh
(where h is any hex digit) to represent non-ASCII characters e.g.\xc3\xa2
is the UTF-8 encoding of the small a with circumflex accent. Show us a representative sample of such output. Also tell us the exact error message(s) that you get from that sample script.Update: It appears that you have data encoded in
cp1252
or similar (Latin1
akaISO-8859-1
is as rare as hen's teeth on Windows). To get that intoUTF-8
using Python, you'd dofixed_data = data.decode('cp1252').encode('utf8')
; I can't help you with C# -- you may like to ask a separate question about that.假设您使用的是 python,请确保使用 Unicode 字符串。
例如:
编辑:
以下是从链接站点读取 UTF-8 文件的示例:
Assuming you're using python, make sure you are using Unicode strings.
For example:
Edit:
Here's an example of reading from a UTF-8 file from the linked site: