python-2.7 - 将 String.startswith() 与 unicode 字符串一起使用

标签 python-2.7 unicode

我正在尝试使用 BeautifulSoup 修剪从 HTML 页面抓取的字符串。它以

开头
 – 

在 html 页面中。我正在尝试以下代码:

if thestring.startswith(unichr(160) + '-' + unichr(160)):
    print "found starting sequence"
    thestring= thestring[3:]

但是,if 条件没有被触发(我的打印语句没有发生这一事实证实了这一点)。我该如何设置该条件?

(同样在某些情况下,字符串最初只是这 3 个字符,在这种情况下我希望字符串以空字符串结束——这样做会成功还是我需要单独测试这种情况?)

最佳答案

通过使用 thestring.__repr__() 我能够确定 bs 给我的是一个以 u'\xa0\u2013\xa0' 开头的字符串

然后下面的代码对我有用:

if thestring.startswith(u"\xA0\u2013\xA0"):
    print "found starting sequence"
    thestring = thestring[3:]

关于python-2.7 - 将 String.startswith() 与 unicode 字符串一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30922485/

相关文章:

python-2.7 - np.squeeze 从形状 (1,1) 之后返回的形状 () ndarray 是什么?

python - 将训练和测试数据保存在文件中

python-2.7 - 如何将字符串列表从 csv 文件转换为 float ?

php - Unicode表情符号不显示在浏览器中但存储在mysql中

python - 如何创建二维 CellRendererToggle

python - 我很难在 python 中使用 mincemeat 进行 mapreduce 来计算不同文件的字数

php mb_strtolower 给出无效字符

C++ 保存带有 unicode 名称问题的文件 - 如何以跨平台方式正确保存 UTF-8 文件名?

c++ - 来自 WM_CHAR 的奇怪输入 - WinAPI

C++ 函数判断 unicode 点是否为 'letter' 而不是标点符号的数量