Removing substring of from a list of strings

前端 未结 4 1873
长发绾君心
长发绾君心 2021-01-29 07:02

There are several countries with numbers and/or parenthesis in my list. How I remove these?

e.g.

\'Bolivia (Plurinational State of)\' should be \'Bolivi

4条回答
  •  孤独总比滥情好
    2021-01-29 07:28

    You can remove string by this way:-

    Remove numbers:-

    import re
    a = 'Switzerland17'
    pattern = '[0-9]'
    res = re.sub(pattern, '', a)
    print(res)
    

    Output:-

    'Switzerland'
    

    Remove parenthesis:-

    b = 'Bolivia (Plurinational State of)'
    pattern2 = '(\s*\(.*\))'
    res2 = re.sub(pattern2, '', b)
    print(res2)
    

    Output:-

    'Bolivia'
    

提交回复
热议问题