【问题标题】:Decode � to real character解码?真实性格
【发布时间】:2012-10-21 08:26:29
【问题描述】:

当我从 twitter 的 Stream API 读取数据然后写入 xmlfile 时。

但是像�这样的特殊字符会导致错误(我的意思是当我在Chrome中打开那个xml文件时,Chrome说那个字符有错误!)

我想在写入 xmlfile 之前将该编码序列 (�) 转换为真实字符 ()!

如何实现?

-------------已添加--------------

这是 XMLFile 的内容:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<text>@carlyraejepsen would be a dream if you follow me, please follow me?, I love you so much you're my inspiration</text>
<text>someone please bring me a caramel apple and a mocha from black cat. i'll love you forever</text>
<text>“@G_MartinFlyKick: Marry me Juliet.I love you and that's all I really know.”&#55357;&#56834;&#55357;&#56834;&#55357;&#56834;&#55357;&#56834;&#55357;&#56834;</text>
<text>"I need to see a picture of him cuz Im trying to imagine you guys making love and all I see is u climbing on top of a big question mark"lmao</text>
<text>@District3music hi, I LOVE YOU follow me please? &amp;lt;3 xx 23</text>
<text>RT @syardley_: So appreciative of my family and people I love, wouldn't be where I am without them. #thankful</text>
<text>#DISTRICT3HALLOWEENFOLLOWSPREE #DISTRICT3HALLOWEENFOLLOWSPREE #3EEKERFROMTHENETHERLANDS love you! Please follow ? @District3music x42</text>
<text>Arguably my favorite electronic music producer @Kluteuk is coming back to Toronto on Dec 22nd. So stoked. Guy has made so many tunes I LOVE.</text>
<text>The stakes are high, the water's rough, but this love is ours.</text>
<text>@NiallOfficial Answer me, I love you very much. Venezuela loves. jhgj</text>
<text>Love this shit http://t.co/qSP79NKx</text>
</root>

这是来自 Chrome 的错误:

This page contains the following errors:

error on line 5 at column 91: xmlParseCharRef: invalid xmlChar value 55357
Below is a rendering of the page up to the first error.

【问题讨论】:

标签: xml unicode


【解决方案1】:

字符引用&amp;#55357; 表示代理代码点(U+D83D),因此尝试将其转换为字符是错误的。它不是一个字符,甚至不是半个字符。

您需要回溯到生成参考的点。原因可能是字符编码混乱。在 UTF-16 中,代理代码单元可能会出现,但当数据被解释为字符时必须成对处理,例如转换为另一种编码或转换为字符引用。

【讨论】:

  • 我从这个链接获取数据:stream.twitter.com/1.1/statuses/…,那么,编码混乱是怎么发生的?
  • @Songokute,很难说,因为页面提示输入用户名和密码。
  • 从XMLFile的内容来看,数据中似乎包含了U+1F602“?”这样的字符,也就是说它占用了UTF-16的两个编码单元。显然原始数据是UTF-16,应该先转成UTF-8。
  • 抱歉回复晚了!但是您可以使用您的推特帐户登录! (因为这是 twitter 流 API)。而且,您的意思是我应该将 XMLFile 的编码从 UTF-8 更改为 UTF-16?
  • 是的,我做到了,谢谢@Korpela,我在写下来之前将 xmlObject 的编码设置为 UTF-16 :)
【解决方案2】:

您可以在服务器响应后使用正则表达式替换它。 python中的简单示例:

import re 
pattern = re.compile(r'&#')
new_content = pattern.sub(' ', SERVER_RESPONSE)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-07
    • 2010-12-30
    • 2014-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-04
    相关资源
    最近更新 更多