function - Parsing(removing extra space,encoding) multiple attributes through same funtion in python -


i trying parse data html file. extract various information , store in respective attributes.however before printing results/attributes in csv file need remove unwanted special characters , encode attribute values.

address2='#4th avenue' price='price $45'   address2 = address2.encode('utf-8') address2= re.sub('[,!@#$&]','',address2) address2=address2.lstrip() address2=address2.rstrip() address2=address2.strip()   price = price.encode('utf-8') price= re.sub('[,!@#$&]','',price) price=price.lstrip() price=price.rstrip() price=price.strip() 

expected output:

price:       price 45 address2:    4th avenue 

i getting result expected need apply strip , encode each , every attribute. there way create function can pass attributes in function . instead of applying strip/encode each attribute.

something

def cleanup(temp):           temp = temp.encode('utf-8')         temp= re.sub('[,!@#$&]','',temp)         temp=temp.lstrip()         temp=temp.rstrip()         temp=temp.strip()         return temp 

so can pass required attribute function

cleanup(address2) cleanup(price) 

to clean output

price:       price 45 address2:    4th avenue 

since new python not sure right way of achieving this.please me on this.

just use return value passing original string function:

def cleanup(temp):    return temp.strip().translate(none,",!@#$&").encode("utf-8")  address2='#4th avenue' price='price $45'  address2 = cleanup(address2) price = cleanup(price) print address2 print price 

output:

4th avenue price 45 

you don't need use lstrip, strip , rstrip, strip strip both sides of string.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -