Python Pandas:sre_constants.error:missing),位置10的未终止的子模式

问题描述:

我想迭代一个使用dataframe1的字符串列表来检查另一个dataframe2是否有在dataframe1中找到的任何字符串来替换它们。Python Pandas:sre_constants.error:missing),位置10的未终止的子模式

for index, row in df.iterrows(): 
    print(row['x1']) 
    df2['strings'].str.replace(row['x1'],"") 

为了做到这一点,我使用上面所示的代码检查和更换为DF1发现

wait_timeout 
interactive_timeout 
pool_recycle 
.... 
__all__ 
folder_name 
re.compile('he(lo') <= error string 

但是任何字符串迭代,而迭代它试图替换字符串re.compile('he(lo')但无法作为字符串有“(()”括号这是不平衡的。我看过其他讨论的是替代使用reg表达式中,我可以通过使用修复/(。所以我试图用

replace = row['x1'].str.replace("(","\(") 
replace = replace.str.replace(")","\)") 

但我收到replace = replace.str.replace(")","\)")错误,指出AttributeError: 'str' object has no attribute 'str'

感谢先进!

在关于此错误:

AttributeError: 'str' object has no attribute 'str'

你已经分配一个字符串对象replace,所以它没有所谓的str任何属性。

您可以修复与replace = replace.replace(")","\)"),虽然你可能要考虑重新命名该replace变量例如为:some_other_name = replace.replace(")","\)")

+0

噢噢噢。这是我的一个愚蠢的错误,我实际上试图使用replace.replace,它导致了一个错误,所以我认为这个错误是在别的地方。 一旦我实现它另一个奇怪的错误发生'sre_constants.error:未终止的字符集在位置33'单词'os.path.split(name.co_filename)[],name.co_firstlineno,name.co_name'字符串似乎()和[]很好。 – WhiteSolstice