【发布时间】:2019-07-04 08:04:49
【问题描述】:
我有一个数据框,其中txt 列包含一个列表。我想使用函数 clean_text() 清理 txt 列。
data = {'value':['abc.txt', 'cda.txt'], 'txt':['[''2019/01/31-11:56:23.288258 1886 7F0ED4CDC704 asfasnfs: remove datepart'']',
'[''2019/02/01-11:56:23.288258 1886 7F0ED4CDC704 asfasnfs: remove datepart'']']}
df = pandas.DataFrame(data=data)
def clean_text(text):
"""
:param text: it is the plain text
:return: cleaned text
"""
patterns = [r"^{53}",
r"[A-Za-z]+[\d]+[\w]*|[\d]+[A-Za-z]+[\w]*",
r"[-=/':,?${}\[\]-_()>.~" ";+]"]
for p in patterns:
text = re.sub(p, '', text)
return text
我的解决方案:
df['txt'] = df['txt'].apply(lambda x: clean_text(x))
但我遇到以下错误: 错误
sre_constants.error: nothing to repeat at position 1
【问题讨论】:
-
@sophros,这个问题不一样。
-
有什么不同呢?错误是一样的。