通过内容功能在Python中的另一个模块 [英] Pass Content To Function of Another Module in Python
问题描述
我使用SAX解析器。我试图发送'内容'我检索使用下面code:
I am using SAX Parser. I am trying to send the 'content' I retrieved using below code:
检查的startElement和endElement之后,我有低于code:
After checking the startElement and endElement, I have the below code:
def characters(self, content):
text = format.formatter(content)
这format.formatter预计将读取该数据,我被作为内容进行任何处理,如消除垃圾字符等,并返回。我这样做,通过使用与string.replace功能:
this format.formatter is expected to read this data that I sent as 'content' for any processing like removing junk characters etc and return it. I do that by using string.replace function:
remArticles = {' ! ':'', ' $ ':''}
for line in content:
for i in remArticles:
line= line.replace(i, remArticles[i])
#FormattedFileForIndexing.write(line)
return line
但是输出不上来了预期。
However the output is not coming up as expected.
这将是巨大的,如果有人可以帮助这一点。
It will be great if some one can help on this.
源将一些事情,如:
噢!那是很多和$$$$ 1000
"Oh! That's lots and 1000s of $$$$"
预计:哦,那是很多1000的
Expected: Oh That's lot of 1000s
推荐答案
您遍历每个字符不是每一行:
You are iterating over each character not each line:
def characters(content):
remArticles = {'!': '', '$': ''} # remove spaces from " ! "
for i in remArticles:
content = content.replace(i, remArticles[i])
return content
您也尝试匹配!
和 $
与周围空间而根据您的预计输出不正确
You are also trying to match !
and $
with spaces around them which according to your expected output is incorrect.
In [6]: content = "Oh! That's lots and 1000s of $$$$"
In [7]: characters(content)
Out[7]: "Oh That's lots and 1000s of "
只需使用取而代之的是最有效的选择:
Just using replace is the most efficient option:
In [20]: timeit characters(content)
1000000 loops, best of 3: 746 ns per loop
In [21]: timeit format_this(content)
100000 loops, best of 3: 2.57 µs per loop
这篇关于通过内容功能在Python中的另一个模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!