我有文件的完整路径列表中是这样的:如何摆脱使用python
a = ['home/robert/Documents/Workspace/datafile.xlsx', 'home/robert/Documents/Workspace/datafile2.xls', 'home/robert/Documents/Workspace/datafile3.xlsx']
什么,我想要的是得到的只是文件名没有扩展名,如:
b = ['datafile', 'datafile2', 'datafile3']
我曾尝试是:
xfn = re.compile(r'(\.xls)+')
for name in a:
fp, fb = os.path.split(fp)
ofn = xfn.sub('', name)
b.append(ofn)
但它会导致:
b = ['datafilex', 'datafile2', 'datafile3x']
为此使用'str.replace()'。它很多,**很多**更快 – Amelia 2013-04-06 10:05:49
你有没有使用正则表达式? 'os.path.splitext'删除扩展名... – Ben 2013-04-06 10:07:31