-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse_txt_files.py
59 lines (51 loc) · 1.68 KB
/
parse_txt_files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# import pdftotext
import os
#
#
# def get_pdf_content(in_file_path, out_file_path):
# with open(in_file_path, 'rb') as f:
# pdf = pdftotext.PDF(f)
#
# result = {'pages':[]}
#
# # Iterate over all the pages
# for i, content in enumerate(pdf):
#
# path = os.path.join(out_file_path, str(i) + '.txt')
# with open(path, 'wb') as file:
# file.write(str(content).encode('utf-8'))
#
# return result
def read_results_from_files(root):
filenames = os.listdir(root)
result = {}
for filename in filenames:
content = read_results_from_file(os.path.join(root, filename)).decode('utf-8')
try:
page_number = int(content[:content.index('\n')].split()[-1])
except:
try:
page_number = int(content[:content.index('\n')].split()[0])
except:
page_number = None
index = filename.split(".")[0]
result[index] = {'index': index,
'page_number': page_number,
'content': content}
return result
def read_results_from_file(path):
with open(path, 'rb') as file:
return file.read()
def print_dict(dict):
for key in dict:
page = dict[key]
print('Index: %s' % key)
print('Page number: %s' % page['page_number'])
print('Content:\n\n' + page['content'])
print('\n\n\n')
if __name__ == '__main__':
in_path = 'Data Structures and Algorithms in Python.pdf'
out_path = 'Data Structures and Algorithms in Python'
#content = get_pdf_content(in_path, out_path)
results = read_results_from_files(out_path)
print_dict(results)