-
Notifications
You must be signed in to change notification settings - Fork 0
/
birdlog.py
124 lines (89 loc) · 2.97 KB
/
birdlog.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import sys
import sqlite3 as lite
import csv
class BirdRFID:
def __init__(self, uniqid, second, minute, hour, day, month, year, birdid, area, submitter):
self.uniqid = uniqid
self.second = second
self.minute = minute
self.hour = hour
self.day = day
self.month = month
self.birdid = birdid
self.area = area
self.submitter = submitter
def dbConnect(query):
try:
con = lite.connect('birds.db')
with con:
cur = con.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS Birds (uniqid TEXT, second TEXT, minute TEXT, hour TEXT, day TEXT, month TEXT, year TEXT, birdid TEXT, area TEXT, submitter TEXT)")
cur.execute("CREATE UNIQUE INDEX IF NOT EXISTS uniqid_idx ON Birds (uniqid)")
cur.execute(query)
con.commit()
except lite.Error, e:
if con:
con.rollback()
print "Error %s:" % e.args[0]
sys.exit(1)
finally:
print "Adding row to main database birds.db..."
con.commit()
if con:
con.close()
def importCSV():
print "Importing file..."
filename = "BIRDRFID.LOG"
birdArray = []
eachBird = []
with open(filename) as f:
for line in f:
birdArray.append([str(n) for n in line.strip().split(' ')])
area = promptExtra()
for row in enumerate(birdArray):
try:
second = row[1][0]
minute = row[1][1]
hour = row[1][2]
day = row[1][3]
month= row[1][4]
year = row[1][5]
birdid = row[1][6]
area = area
i = str(row[0])
uniqid = i+second+minute+hour+day+month+year+i
print uniqid
eachBird.append({'uniqid': uniqid, 'second' : second, 'minute' : minute, 'hour' : hour, 'day' : day, 'month' : month, 'year' : year, 'birdid' : birdid, 'area' : area[0], 'submitter' : area[1]})
except IndexError:
print "A line in the file doesn't have enough entries."
for bird in eachBird:
columns = ', '.join(bird.keys())
placeholders = '"'+'", "'.join(bird.values())
placeholders += '"'
query = 'INSERT OR IGNORE INTO Birds (%s) VALUES (%s)' % (columns, placeholders)
dbConnect(query)
print "Added to birds.db"
var = raw_input('Would you like to back this up into the master csv? (y/n): ')
if var is 'y':
print "Backing up to csv file bird-master.csv..."
masterWrite()
elif var is 'n':
print "Not backed up to csv file bird-master.csv"
else:
print "Unknown input, file not backed up to csv file bird-master.csv"
sys.exit()
def promptExtra():
area = raw_input('Which area were these recorded?: ')
submitter = raw_input('Who is submitting this data?: ')
return area, submitter
def masterWrite():
conn = lite.connect('birds.db')
conn.text_factory = str ## my current (failed) attempt to resolve this
cur = conn.cursor()
data = cur.execute("SELECT * FROM Birds")
with open('bird-master.csv', 'wb') as f:
writer = csv.writer(f)
writer.writerow(['Uniqe ID', 'Second', 'Minute', 'Hour', 'Day', 'Month', 'Year', 'Bird ID', 'Area', 'Submitter'])
writer.writerows(data)
print "Data added to bird-master.csv"
importCSV()