Reputation: 33213
Can anyone help me with the following query what is the pythonic way to cleanse the following strings:
Lets say I have the word
"abcd
or
'blahblah
then the words actually are
abcd, blahblah
I can think of a basic way.. but actually I am reading a huge text file.. and explicitly writing a code to read char by char seems like overkill and definitely not pythonic.. I am sure there is a pythonic way to do this..:) Thanks
Upvotes: 0
Views: 454
Reputation: 22633
Looks like you want just the alphabetical characters from each word.
import re
_regex = r'\W+' #word characters only
#read in input
#split input on ' ' (space), to get words
for word in list_of_words:
word = re.sub(_regex, '', word)
Upvotes: 1
Reputation: 387507
You can strip unwanted characters from the beginning and end of a string using the str.strip()
method.
>>> '"abcd'.strip( '"\'' )
'abcd'
>>> '\'blahblah'.strip( '"\'' )
'blahblah'
>>> print( '"abcd'.strip( '"\'' ) )
abcd
>>> print( '\'blahblah'.strip( '"\'' ) )
blahblah
Upvotes: 2