python:读取具有文件名和文件概念的文件夹

时间:2016-12-14 22:42:57

标签: python xml list contain

我有一个数据框(下面),列'name', 我想删除(例如第一行)

'/Users/xccxken/Desktop/NNRelease/paperVersion/'

'.txt'

保持单词(第一行示例)

'Topic+Topic_of_Situation.shortageglut'
每行

,n_1,n_2,name
0,water,shortage,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Situation.shortageglut.txt
1,supply,shortage,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Situation.shortageglut.txt
2,skill,shortage,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Situation.shortageglut.txt
214,income,policy,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.rules.legal.txt
215,immigration,policy,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.rules.legal.txt
216,health,policy,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.rules.legal.txt
485,license,agreement,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.deal.txt
486,lease,agreement,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.deal.txt
487,immunity,agreement,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.deal.txt
488,franchise,agreement,/Users/xccxken/Desktop/NNRelease/paperVersion/Topic+Topic_of_Plan&Deal&Rules.deal.txt

1 个答案:

答案 0 :(得分:0)

您可以使用.str.strip()方法:

prefix = '/Users/xccxken/Desktop/NNRelease/paperVersion/'
suffix = '.txt'
df['name'] = df['name'].str.rstrip(suffix).str.lstrip(prefix)

或正则表达式:

description = r'([^/]+)\.txt'
df['name'] = df['name'].str.extract(description)