只需阅读文件。这会给你一个字符串:
In [2]: with open('encase_example.md') as cf:
...: data = cf.read()
...:
In [3]: data[:41]
Out[3]: '\n1)\nName\tfile.doc\nFile Category\tDocument\n'
(仅显示部分字符串作为示例。)
请注意,在数据中,每条记录的字段之间有换行符,但每个字段的键和值之间有制表符。我们稍后将使用它。
这适用于正则表达式:
In [14]: re.findall('Full Path.*', data)
Out[14]:
['Full Path\tproject\\D\\analysis\\system\\folder\\file.doc',
'Full Path\tproject\\D\\analysis\\system\\folder\\file2.doc']
如果要分隔记录,只需拆分\n\n
:
In [18]: records = data.split('\n\n')
In [19]: len(records)
Out[19]: 2
In [20]: records[0][:50]
Out[20]: '\n1)\nName\tfile.doc\nFile Category\tDocument\nFile Type'
您还可以将记录制作成字典:
In [35]: dict([ln.split('\t') for ln in records[0].splitlines()][2:])
Out[35]:
{'Entry Modified': '12/18/14 11:18:53AM',
'File Acquired': '04/28/15 01:54:45PM',
'File Category': 'Document',
'File Created': '03/29/14 03:22:59PM',
'File Deleted': '',
'File Type': 'Word Document',
'Full Path': 'project\\D\\analysis\\system\\folder\\file.doc',
'Is Deleted': '',
'Last Written': '08/18/08 01:20:48PM',
'Name': 'file.doc',
'Physical Location': '546,930,589,696',
'Physical Size': '32,768'}