Pass Content To Function of Another Module in Python

I am using SAX Parser. I am trying to send the 'content' I retrieved using below code:

After checking the startElement and endElement, I have the below code:

def characters(self, content):  
   text =  format.formatter(content)

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.

source will some thing like:

"Oh! That's lots and 1000s of $$$$"

Expected: Oh That's lot of 1000s

Answers


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

Need Your Help

Modifying a Compiled Static Library

c++ objective-c c ios xcode

Put on your c++ ninja voodoo gloves.

Style buttons and tabs in header

jquery html css angularjs

I don't have too much experience with front end so I'm trying to learn and I'm running into an issue that probably has a simple solution. I have 4 components, 2 buttons and 2 tabs. I want them styl...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.