这周开始学习python,所以我想我应该用它而不是excel来解析文件路径中的一些字段。在
我有大约3000个文件都符合命名规则。 /家庭/姓氏.FirstName.Account.Doctype.Date.extension在
例如,其中一个文件可能命名为:科斯比,比尔…简介.2006.doc 完整路径是/Volumes/HD/Organized Files/Cosby,Bill/科斯比,比尔…简介.2006.doc
在这种情况下:
科斯比,比尔是家里人
其中household(Cosby,Bill)是实际文件的附加文件夹
比尔是名字
Cosby将是姓氏
账户字段被省略
Profile是doctype
2006年是日期
doc是扩展名
所有这些文件都位于这个目录/Volumes/HD/Organized files/I used terminal and ls将所有文件的列表放入我桌面上的一个.txt文件中,我正试图将文件路径中的信息解析为与上面示例中类似的类别。理想情况下,我想输出到一个csv,每个类别有一个列。这是我丑陋的代码:
def main():
file = open('~/Desktop/client_docs.csv', "rb")
output = open('~/Desktop/client_docs_parsed.txt', "wb")
for line in file:
i = line.find(find_nth(line, '/', 2))
beghouse = line[i + len(find_nth(line, '/', 2)):]
endhouse = beghouse.find('/')
household = beghouse[:endhouse]
lastn = (line[line.find(household):])[(line[line.find(household):]).find('/') + 1:(line[line.find(household):]).find('.')]
firstn = line[line.find('.') + 1: line.find('.', line.find('.') + 1)]
acct = line[line.find('{}.{}.'.format(lastn,firstn)) + len('{}.{}.'.format(lastn,firstn)):line.find('.',line.find('{}.{}.'.format(lastn,firstn)) + len('{}.{}.'.format(lastn,firstn)))]
doctype_beg = line[line.find('{}.{}.{}.'.format(lastn, firstn, acct)) + len('{}.{}.{}.'.format(lastn, firstn, acct)):]
doctype = doctype_beg[:doctype_beg.find('.')]
date_beg = line[line.find('{}/{}.{}.{}.{}.'.format(household,lastn,firstn,acct,doctype)) + len('{}/{}.{}.{}.{}.'.format(household,lastn,firstn,acct,doctype)):]
date = date_beg[:date_beg.find('.')]
print '"',household, '"','"',lastn, '"','"',firstn, '"','"',acct, '"','"',doctype, '"','"',date,'"'
def find_nth(body, s_term, n):
start = body[::-1].find(s_term)
while start >= 0 and n > 1:
start = body[::-1].find(s_term, start+len(s_term))
n -= 1
return ((body[::-1])[start:])[::-1]
if __name__ == "__main__": main()
它似乎可以工作,但我遇到了问题,当有另一个封闭的文件夹,它然后转移我的所有字段。。例如,当
/Volumes/HD/有组织的文件/Cosby,Bill/
它的at/Volumes/HD/Organized Files/redired/Cosby,Bill/
我知道必须有一个不那么笨拙的方法来解决这个问题。在
据我所知,我相信这将是一个解决方案,它不依赖于以前编译的文件列表
这使用os库来“遍历”家庭列表。然后为每个“家庭”收集一份文件清单。如果使用这个列表,在csv文件中生成记录,将文件名分开,使用句点作为分隔符。在
它使用csv库生成输出,这看起来有点像
^{pr2}$如果不需要扩展名,则可以通过更改行来修改它:
到
它告诉它只使用文件名的最后一部分,然后从headerRow中删除“Extension”字符串。在
希望这有帮助
现在还不清楚问题是什么,但同时,这里有一些东西可以让你开始:
输出:
^{pr2}$另一种解释是,您希望将每个字段存储在一个参数中 另外一条路把事情搞砸了。。。在
这是
row
在for循环中的样子:因此,解决办法可能是逆向工作。在
将},依此类推。在
row[-1]
分配给extension
,row[-2]
分配给{{>
rstrip()
结果
^{pr2}$编辑1
这是另一个非常实用的工具:regex
结果
编辑2
我想知道当我发布最后一次编辑时我的大脑在哪里。以下功能也可以实现:
相关问题 更多 >
编程相关推荐