2013-08-07 23 views
1

我想知道是否有更好的更快的方法來清理這個返回的字符串。或者這是最好的方法。它可以工作,但總是需要更高效的方法。更好的代碼,然後正則表達式子()python 2.7

我有一個返回以下輸出功能:

"("This is your:, House")" 

我在打印前清理:

a = re.sub(r'^\(|\)|\,|\'', '', a) 
print a 

>>> This is your: House 

我也是從不同的方式的人做的事情中學到很多東西。

回答

2

您不需要使用正則表達式來執行此操作。

>>> import string 
>>> a = '"("This is your:, House")"' 
>>> ''.join(x for x in a if x not in string.punctuation) 
'This is your House' 

>>> tbl = string.maketrans('', '') 
>>> a.translate(tbl, string.punctuation) 
'This is your House' 
0
s='"("This is your:, House")"' 
s.replace('\"','').replace('(','').replace(')','').replace(',','').replace(':','') 
'This is your House'