【问题标题】:How to remove special characters in python?如何删除python中的特殊字符?
【发布时间】:2014-01-31 06:24:18
【问题描述】:

由于描述中的特殊字符(�),我在下面的代码中遇到问题。由于这些特殊字符,我遇到了错误。

错误:“ascii”编解码器无法解码位置 821 中的字节 0x94:序数不在范围内 (128) 请帮我消除这个错误。

下面分别给出文本形式的数据和html形式的数据

链接:http://m.cellularoutfitter.com/p-85870-anycom-solar-bluetooth-car-kit_c.html(页尾有说明)

我尝试了各种方法和编码,但都失败了。

首先我得到完整的链接源然后通过使用 x 路径我得到一个变量中的描述。 由于某些原因,我无法发布完整的代码。对不起

Python 代码:

parser = etree.HTMLParser(remove_blank_text=True, encoding="utf-8")
tree = etree.HTML(popup_html, parser)
    description = tree.xpath("//span[@itemprop='description' and not(src)] ")

log.debug(str(description[0]))
for desc in description:
    log.debug(etree.tostring(desc,encoding='UTF-8'))
    if etree.tostring(desc,encoding='UTF-8').find("IFRAME") < 0:
        reply_dict['product_desc'] = reply_dict['product_desc'] + etree.tostring(desc,encoding='UTF-8')
        reply_dict['product_desc'] = reply_dict['product_desc'].replace("&#13;\n", "").replace("\n", "<br/>").replace("img","").replace('< src="/productPics/altImgs/decal-skin-pdp-2.jpg"/>',"")
        reply_dict['product_desc'] = reply_dict['product_desc'].replace("\xef\xbf\xbd","'")
        reply_dict['product_desc'] = reply_dict['product_desc'].replace("\x92","'")
        reply_dict['product_desc'] = "<br />".join(reply_dict['product_desc'].split("\n")).replace("     ", "&nbsp;").encode('ascii', 'xmlcharrefreplace')

HTML 代码:

<div class="centerContain">
            Convenient Bluetooth car kit easily mounts to vehicle windshield and features high-performance solar panel capable of converting UV rays into Bluetooth battery power. What's included: ANYCOM Solar Bluetooth Car Kit, window mount, suction cups, 12/24V vehicle power adapter w/USB cable, 3M adhesive tape, user guide.
            <ul>
                <li>Solar panel recharges battery, providing 30 minutes of talk time for every 3 hours of sun light</li><li>Features Digital Signal Processing (DSP) technology, including compression and echo cancellation</li><li>Easily pairs with compatible devices</li><li>Bluetooth: v2.0</li><li>Talk Time: 15 hours</li><li>Standby Time: 25 days</li><li>Operating Range: 33 ft. (10 meters)</li><li>Size: 3.59� (H) x 1.98� (W) x 0.52� (D)</li><li>Weight: 2.11 oz.</li><li>Warranty: ANYCOM limited worldwide 2-year warranty</li>
            </ul>
        </div>

以文本形式:

方便的蓝牙车载套件可轻松安装到车辆挡风玻璃上,并配备高性能太阳能电池板,能够将紫外线转换为蓝牙电池供电。包括:ANYCOM 太阳能蓝牙车载套件、窗架、吸盘、带 USB 电缆的 12/24V 车载电源适配器、3M 胶带、用户指南。 太阳能电池板为电池充电,每 3 小时的阳光照射可提供 30 分钟的通话时间 采用数字信号处理 (DSP) 技术,包括压缩和回声消除 轻松与兼容设备配对 蓝牙:v2.0 通话时间:15小时 待机时间:25天 工作范围:33 英尺(10 米) 尺寸:3.59� (H) x 1.98� (W) x 0.52� (D) 重量:2.11 盎司。 保修:ANYCOM 全球有限保修 2 年

【问题讨论】:

  • 你的代码在哪里?您需要做的是跳过不适合有效范围的字符,或者捕获异常。
  • 该页面的文本以 iso-8859-1 编码,但元标题显示为 utf8,如果您将编码更改为 iso-8859-1,浏览器将无法识别该字符将能够将该字符转换为 unicode

标签: python unicode special-characters decode


【解决方案1】:

代码不清楚,但我认为问题在于解码,它应该在解码为 utf-8 后工作

例如,

string = '(10 meters) Size: 3.59� (H) x 1.98� (W) x 0.52� (D) Weight:'.decode('utf-8')

【讨论】:

    猜你喜欢
    • 2015-11-11
    • 2015-08-22
    • 2011-11-13
    • 2020-06-06
    • 2011-11-20
    • 1970-01-01
    • 2010-10-14
    • 2022-08-03
    相关资源
    最近更新 更多