【发布时间】:2014-03-17 15:04:20
【问题描述】:
这里是dealio:我编写了一个程序来查找字典中的所有算法类。但是,我在处理重音字符时遇到了问题。目前我的代码将它们读入,将它们视为不可见,但仍会在末尾以 '\xc3\???' 的形式打印出某种替换代码。我想丢弃所有带重音的单词,但我不知道如何检测它们。
我尝试过的事情:
- 检查类型是否为 unicode
- 使用正则表达式检查包含“\xc3”的单词
- 解码/编码(我不完全理解 unicode,但我尝试过的任何方法都不起作用)。
问题/问题:我需要了解如何检测重音符号,但我的程序将重音符号打印到命令行上,显示为奇怪的 '\xc3\???'字符,这不是程序处理它们的方式,因为我找不到任何包含 '\xc3\???' 的单词尽管已打印到命令行。
示例:sé -> s\xc3\xa9,我的程序将 sé 和 s 视为字谜。
测试词典:
stop
tops
pots
hello
world
pit
tip
\xc3\xa9
sé
s
se
代码输出:
Found
\xc3\xa9
['pit', 'tip']
['world']
['s\xc3\xa9', 's']
['\\xc3\\xa9']
['stop', 'tops', 'pots']
['se']
['hello']
程序本身:
import re
anadict = {};
for line in open('fakedic.txt'):#/usr/share/dict/words'):
word = line.strip().lower().replace("'", "")
line = ''.join(sorted(ch for ch in word if word if ch.isalnum($
if isinstance(word, unicode):
print word
print "UNICODE!"
pattern = re.compile(r'xc3')
if pattern.findall(word):
print 'Found'
print word
if anadict.has_key(line):
if not (word in anadict[line]):
anadict[line].append(word)
else:
anadict[line] = [word]
for key in anadict:
if (len(anadict[key]) >= 1):
print anadict[key]
帮助?
【问题讨论】:
标签: python regex unicode command-line non-ascii-characters