只有unicode的一部分正在替换中Python。唐我不明白为什么

2024-09-30 14:18:12 发布

您现在位置:Python中文网/ 问答频道 /正文

subject = page.select('div.container h1')
subject = [x.text.replace('2015', '')for x in subject] 
print subject



[u'\u20132016 Art Courses']# This is the code after.
[u'2015\u20132016 Art Courses']#This is the code before.
subject = [x.text.replace('20132016', '')for x in subject]

当我尝试将.replace改为'20132016'时,它只是打印出来 [u'2015\U2013 2016艺术课程']

有人知道如何摆脱20132016以及
课程。你知道吗


Tags: thetextinforispagecodethis
2条回答

\u2013是unicode符号en dash。例如,选中here。你知道吗

因此,要摆脱艺术之外的一切,你需要像这样替换它:

>>> a = u'2015\u20132016 Art Courses'
>>> a.replace(u'2015\u20132016', '').replace('Courses', '').strip()
u'Art'

字符串中没有“2013”字符。您只有一个字符,unicode 2013,即“–”,一个短划线。你需要替换那个角色。你知道吗

x.text.replace(/u'u20132016', '') for x in subject]

相关问题 更多 >