How can we strip punctuation at the start of a string using Python?

早过忘川 提交于 2019-12-01 06:32:23

问题


I want to strip all kinds of punctuation at the start of the string using Python. My list contains strings and some of them starting with some kind of punctuation. And how can I strip all type of punctuation from the strings?

For example: If my word is like ,,gets, I want to strip ,, from the word, and I want gets as the result. Also, I want to strip away spaces as well as numbers from the list. I have tried with the following code but it is not producing the correct result.

If 'a' is a list containing some words:

for i in range (0,len(a)):
      a[i]=a[i].lstrip().rstrip()
      print a[i]

回答1:


You can use strip():

Return a copy of the string with the leading and trailing characters removed. The chars argument is a string specifying the set of characters to be removed.

Passing string.punctuation will remove all leading and trailing punctuation chars:

>>> import string
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'

>>> l = [',,gets', 'gets,,', ',,gets,,']
>>> for item in l:
...     print item.strip(string.punctuation)
... 
gets
gets
gets

Or, lstrip() if you need only leading characters removed, rstip() - for trailing characters.

Hope that helps.




回答2:


Pass the characters you want to remove in lstrip and rstrip

'..foo..'.lstrip('.').rstrip('.') == 'foo'



回答3:


strip() when used without parameters strips only spaces. If you want to strip any other character, you need to pass it as a parameter to strip function. In your case you should be doing

a[i]=a[i].strip(',')



回答4:


To remove punctuation, spaces, numbers from the beginning of each string in a list of strings:

import string

chars = string.punctuation + string.whitespace + string.digits    
a[:] = [s.lstrip(chars) for s in a]

Note: it doesn't take into account non-ascii punctuation, whitespace, or digits.




回答5:


If you want to remove it only from the begining, try this:

    import re
    s='"gets'
    re.sub(r'("|,,)(.*)',r'\2',s)



回答6:


Assuming you want to remove all punctuation regardless of where it occurs in a list containing strings (which may contain multiple words), this should work:

test1 = ",,gets"
test2 = ",,gets,,"
test3 = ",,this is a sentence and it has commas, and many other punctuations!!"
test4 = [" ", "junk1", ",,gets", "simple", 90234, "234"]
test5 = "word1 word2 word3 word4 902344"

import string

remove_l = string.punctuation + " " + "1234567890"

for t in [test1, test2, test3, test4, test5]:
    if isinstance(t, str):
        print " ".join([x.strip(remove_l) for x in t.split()])
    else:
        print [x.strip(remove_l) for x in t \
               if isinstance(x, str) and len(x.strip(remove_l))]



回答7:


for each_string in list:
    each_string.lstrip(',./";:') #you can put all kinds of characters that you want to ignore.


来源:https://stackoverflow.com/questions/22469766/how-can-we-strip-punctuation-at-the-start-of-a-string-using-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!