我正在编写一个python MapReduce字数统计程序.问题是数据中散布着许多非字母字符,我发现这篇文章从Python中的字符串中删除除字母数字字符之外的所有字符,这显示了使用正则表达式的一个很好的解决方案,但我不知道如何实现它
def mapfn(k, v):
print v
import re, string
pattern = re.compile('[\W_]+')
v = pattern.match(v)
print v
for w in v.split():
yield w, 1
Run Code Online (Sandbox Code Playgroud)
我担心我不确定如何使用库re甚至正则表达式.我不确定如何正确地将正则表达式模式应用于传入的字符串(书的行)v以检索没有任何非字母数字字符的新行.
建议?
Perl和其他一些当前的正则表达式引擎在正则表达式中支持Unicode属性,例如类别.例如,在Perl中,您可以使用\p{Ll}匹配任意小写字母或p{Zs}任何空格分隔符.在Python的2.x和3.x行中都没有看到对此的支持(有应有的遗憾).是否有人意识到获得类似效果的好策略?欢迎本土解决方案.
我需要帮助从某些字符串中删除下划线。这并不难,困难在于字符串确实包含日语字符。
\n例如,我有这些字符串(数十万个其他字符串):
\nstr1 = "3F_\xe3\x81\x86_\xe3\x81\x8c_LOW_\xe3\x81\xbe\xe3\x81\x84_\xe3\x81\x8c"\nstr2 = "A5_BB_\xe5\x90\x88_\xe3\x82\x89"\nstr3 = "C1_\xe3\x81\xa0_\xe3\x81\xa8_\xe6\x80\x9d"\nRun Code Online (Sandbox Code Playgroud)\n我想要得到的最终结果是这样的:
\nstrFinal1 = "3F_\xe3\x81\x86\xe3\x81\x8c_LOW_\xe3\x81\xbe\xe3\x81\x84\xe3\x81\x8c"\nstrFinal2 = "A5_BB_\xe5\x90\x88\xe3\x82\x89"\nstrFinal3 = "C1_\xe3\x81\xa0\xe3\x81\xa8\xe6\x80\x9d\nRun Code Online (Sandbox Code Playgroud)\n所以本质上我只想删除两个日语字符之间的下划线。\n我如何在 python 中做到这一点?
\n我正面临一个问题.实际上,我使用越南文本,我想找到包含大写字母(大写字母)的每个单词.当我使用're'模块时,我的函数(temp)不会像"Đà"那样捕捉到单词.另一种方式(temp2)是一次检查每个字符,它可以工作,但它很慢,因为我必须将句子分成单词.
因此,我想知道是否有一种"重新"模块可以捕获所有特殊的大写字母.
我有两种方式:
def temp(sentence):
return re.findall(r'[a-z]*[A-Z]+[a-z]*', sentence)
lis=word_tokenize(sentence)
def temp2(lis):
proper_noun=[]
for word in lis:
for letter in word:
if letter.isupper():
proper_noun.append(word)
break
return proper_noun
Run Code Online (Sandbox Code Playgroud)
输入:
'nous avons 2 ??ng et 3 Euro'
Run Code Online (Sandbox Code Playgroud)
预期产量:
['??ng','Euro']
Run Code Online (Sandbox Code Playgroud)
谢谢!