繁体   English   中英

Python 无法识别空白字符

[英]Python not identifying a white space character

我对这个问题已经接近我的智慧了:基本上,我需要删除单词之间的双空格。 我的程序恰好是希伯来语,但这是基本思想:

TITLE: הלכות ‏ ‏השכמת‏ ‏הבוקר‏

请注意,前两个单词之间有一个额外的空格(Herbew 从右到左阅读)。

我尝试了很多很多不同的方法,这里有一些:

# tried all these with and without unicode
title = re.sub(u'\s+',u' ',title.decode('utf-8'))
title = title.replace("  "," ")
title = title.replace(u"  הלכות",u" הלכות")

直到最后我采取了一种非常不必要的方法(粘贴时一些格式被弄乱了):

def remove_blanks(s):
    word_list = s.split(" ")
    final_word_list = []
    for word in word_list:
        print "word: " +word
        #tried every qualifier I could think of...
        if not_blank(word) and word!=" " and True != re.match("s*",word):
            print "^NOT BLANK^"
            final_word_list.append(word)
    return ' '.join(final_word_list)

def not_blank(s):
    while " " in s:
        s = s.replace(" ","")
    return (len(s.replace("\n","").replace("\r","").replace("\t",""))!=0);

而且,令我大吃一惊的是,这就是我得到的:

word: הלכות
^NOT BLANK^
word: ‏           #this should be tagged as Blank!!
^NOT BLANK^
word: ‏השכמת‏
^NOT BLANK^
word: ‏הבוקר‏
^NOT BLANK^

所以显然我的预选赛没有用。 这里发生了什么?

有一个隐藏的\\xe2\\x80\\x8e,从左到右的标记。 使用 repr(word) 找到它。 谢谢@mgilson!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM