在Python 2.7.3 / Raspberry Pi中使用特殊字符转义HTML

576i

我被困在这里尝试不转义HTML特殊字符。

有问题的文字是

Rudimental & Emeli Sandé

应该转换为基本&EmeliSandé

文本是通过WGET下载的(在Python外部)

要对此进行测试,请在此行中保存一个ANSI文件并导入。

import HTMLParser

trackentry = open('import.txt', 'r').readlines()
print(trackentry)
track = trackentry[0]
html_parser = HTMLParser.HTMLParser()

track = html_parser.unescape(track)

print(track)

当一行中有é时,我会收到此错误

*pi@raspberrypi ~/scripting $ python unparse.py
['Rudimental & Emeli Sand\xe9\n']
Traceback (most recent call last):
  File "unparse.py", line 9, in <module>
    track = html_parser.unescape(track)
  File "/usr/lib/python2.7/HTMLParser.py", line 472, in unescape
    return re.sub(r"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));", replaceEntities, s)
  File "/usr/lib/python2.7/re.py", line 151, in sub
    return _compile(pattern, flags).sub(repl, string, count)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 11: ordinal not in range(128)*

相同的代码在Windows下可以正常工作-我只在运行Python 2.7.3的树莓派上遇到问题。

我在走路

Python无法使用ASCII编解码器解码' é(' \ xe9 '),因为此字符不是7位ASCII。

您的问题(浓缩):

import HTMLParser
parser = HTMLParser.HTMLParser()
input = 'Rudimental &amp; Emeli Sand\xe9'
output = parser.unescape(input)

产生

Traceback (most recent call last):
  File "problem.py", line 4, in <module>
    output = parser.unescape(input)
  File "/usr/lib/python2.7/HTMLParser.py", line 475, in unescape
    return re.sub(r"&(#?[xX]?(?:[0-9a-fA-F]+|\w{1,8}));", replaceEntities, s)
  File "/usr/lib/python2.7/re.py", line 151, in sub
    return _compile(pattern, flags).sub(repl, string, count)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 11: ordinal not in range(128)

HTMLParser.unescape()返回unicode对象,因此必须转换您的输入str因此,它要求提供默认编码(在您的情况下为ASCII),并且无法将' \ xe9 '解释为ASCII字符(因为不是)。我猜您的文件编码为ISO-8859-1,其中“ \ xe9 ”为“ é ”。

有两个简单的解决方案。您可以手动进行转换:

import HTMLParser
parser = HTMLParser.HTMLParser()
input = 'Rudimental &amp; Emeli Sand\xe9'
input = input.decode('iso-8859-1')
output = parser.unescape(input)

或者在处理文件时使用codecs.open()而不是open()

import codecs
import HTMLParser
parser = HTMLParser.HTMLParser()
input = codecs.open("import.txt", encoding="iso-8859-1").readline()
output = parser.unescape(input)

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用OpenCV,Python,Raspberry Pi 3的Ball Tracker

来自分类Dev

Raspberry Pi 3上的nghttp2 w / Buildroot

来自分类Dev

Python,Windows 和 Raspberry pi 3 之间的套接字

来自分类Dev

Raspberry Pi 2 Netflix

来自分类Dev

Raspberry Pi 2 Netflix

来自分类Dev

尝试在Raspberry Pi 2上安装python ping模块

来自分类Dev

Python 2打印无效语法:Raspberry Pi

来自分类Dev

Raspberry Pi 3 BLE扫描

来自分类Dev

Raspberry Pi 3 BLE扫描

来自分类Dev

使用Raspberry Pi 3在Server 16.04上安装Icinga2

来自分类Dev

使用Python 3在(Raspberry Pi的)X服务器上显示全屏jpeg

来自分类Dev

使用Raspberry Pi 3,OpenCV和Python的运动跟踪器

来自分类Dev

使用Python 3在(Raspberry Pi的)X服务器上显示全屏jpeg

来自分类Dev

在Raspberry Pi上使用Python 3,如何调用MPlayer并传递URL

来自分类Dev

使用OpenCV,Python和Raspberry Pi 3的球追踪器(带摄像头模块)

来自分类Dev

在Raspberry Pi 2中使用后如何释放频道?

来自分类Dev

使用i2c,python和raspberry pi读取14位数据

来自分类Dev

在Raspberry Pi 2上使用AT命令和Python接收空白SMS SIM800

来自分类Dev

使用i2c,python和raspberry pi读取14位数据

来自分类Dev

Python Raspberry Pi GPIO错误

来自分类Dev

Raspberry Pi Python ValueError吗?

来自分类Dev

从Raspberry Slave从Raspberry PI PI SPI读取与wireingPI2?

来自分类Dev

如何在Ubuntu Core Raspberry Pi 3中连接到WPA2企业WiFi

来自分类Dev

Raspberry Pi在Python 3脚本中的lower()函数未将输入转换为小写

来自分类Dev

用于ARM处理器(Raspberry Pi)和python3的psutil的替代

来自分类Dev

Raspberry Pi在Python 3脚本中的lower()函数未将输入转换为小写

来自分类Dev

Broken raspberry pi 4 case python3 脚本导致磁盘错误

来自分类Dev

Raspberry Pi 3 到 Azure IoT 中心无法在 python 中连接

来自分类Dev

Python:OSError:[Errno -9985]在Raspberry PI 3B +上使用Snowboy和SpeechRecognition时设备不可用

Related 相关文章

  1. 1

    使用OpenCV,Python,Raspberry Pi 3的Ball Tracker

  2. 2

    Raspberry Pi 3上的nghttp2 w / Buildroot

  3. 3

    Python,Windows 和 Raspberry pi 3 之间的套接字

  4. 4

    Raspberry Pi 2 Netflix

  5. 5

    Raspberry Pi 2 Netflix

  6. 6

    尝试在Raspberry Pi 2上安装python ping模块

  7. 7

    Python 2打印无效语法:Raspberry Pi

  8. 8

    Raspberry Pi 3 BLE扫描

  9. 9

    Raspberry Pi 3 BLE扫描

  10. 10

    使用Raspberry Pi 3在Server 16.04上安装Icinga2

  11. 11

    使用Python 3在(Raspberry Pi的)X服务器上显示全屏jpeg

  12. 12

    使用Raspberry Pi 3,OpenCV和Python的运动跟踪器

  13. 13

    使用Python 3在(Raspberry Pi的)X服务器上显示全屏jpeg

  14. 14

    在Raspberry Pi上使用Python 3,如何调用MPlayer并传递URL

  15. 15

    使用OpenCV,Python和Raspberry Pi 3的球追踪器(带摄像头模块)

  16. 16

    在Raspberry Pi 2中使用后如何释放频道?

  17. 17

    使用i2c,python和raspberry pi读取14位数据

  18. 18

    在Raspberry Pi 2上使用AT命令和Python接收空白SMS SIM800

  19. 19

    使用i2c,python和raspberry pi读取14位数据

  20. 20

    Python Raspberry Pi GPIO错误

  21. 21

    Raspberry Pi Python ValueError吗?

  22. 22

    从Raspberry Slave从Raspberry PI PI SPI读取与wireingPI2?

  23. 23

    如何在Ubuntu Core Raspberry Pi 3中连接到WPA2企业WiFi

  24. 24

    Raspberry Pi在Python 3脚本中的lower()函数未将输入转换为小写

  25. 25

    用于ARM处理器(Raspberry Pi)和python3的psutil的替代

  26. 26

    Raspberry Pi在Python 3脚本中的lower()函数未将输入转换为小写

  27. 27

    Broken raspberry pi 4 case python3 脚本导致磁盘错误

  28. 28

    Raspberry Pi 3 到 Azure IoT 中心无法在 python 中连接

  29. 29

    Python:OSError:[Errno -9985]在Raspberry PI 3B +上使用Snowboy和SpeechRecognition时设备不可用

热门标签

归档