-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeDB.py
More file actions
65 lines (58 loc) · 2.4 KB
/
Copy pathmakeDB.py
File metadata and controls
65 lines (58 loc) · 2.4 KB
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
60
61
62
63
64
65
#---------------------------------------- makeDB.py ------------------------------------------------#
# makedb.py reads a file containing data. The data in the file consists of documents, one per line. #
# Each document is a number of key: value pairs. Here it is assumed that all values are integers. #
# makedb.py reads such a file and stores each document as a python dictionary. #
# The entire database is stored as a list of such dictionaries in binary form. #
# USAGE #
# import makeDB.py then use writeDB(inFile, out) to write the database and store it to disk #
# inFile - the data file. #
# out - the binary file representing the database #
# to load the database into a list use l = loadDB(dbfile) #
# Where dbfile is the file containing the binary representation of the database, this is the #
# same file as the out argument to writeDB(inFile, out) #
#---------------------------------------------------------------------------------------------------#
import pickle
def readData(dataFile):
with open(dataFile, 'r') as f:
l = []
for line in f:
w = []
for word in line.split():
if word.endswith(':'):
word = word[:-1]
w.append(word)
l.append(w)
return l
def dictify(dataFile):
l = readData(dataFile)
n = []
j = 1
for line in l:
i = iter(line)
b = dict(zip(i,i))
b['ID'] = j
j += 1
n.append(b)
#convert all values for all key, value pairs in every dictionary to an integer
for dct in n:
for key in dct:
dct[key] = int(dct[key])
return n
def writeDB(inFile, out):
l = dictify(inFile)
with open(out, "wb") as f:
pickle.dump(l, f , -1)
def loadDB(dbfile):
with open(dbfile, "rb") as f:
l = pickle.load(f)
return l
#For Testing
#def main():
# writeDB("data.txt", "final")
# with open("final", "rb") as f:
# n = pickle.load(f)
# n = loadDB("final")
# print(n)
# for line in n:
# print(line)
#main()