For some reason my code works for all the 10 digit numbers but not for the 3-4 digit numbers. I created a sample concept to try to simplify this so that I could see where my logic went wrong. But my sample works perfectly, yet my actual code doesn't. My conclusion is that it has to be something with reading data from SQLite or CSV. Here is the sample concept code which works great:
test_list = [ (1, 1234567890), (2, 987654321), (3, 123), (4, 4567) ]
# showing all contents
print test_list
# proving it is a list
print type(test_list)
# comparison list
new_list = [ 1234567890, 987654321, 123, 4567, 1567839890, 987654321 ]
# find k given matching v
for num in new_list:
for k, v in test_list:
if v == num:
print 'the key of ' + str(num) + ' = ' + str(k)
Here is the non-working code:
def populateTextMsgs(csvfile, db):
conn = sqlite3.connect(db)
conn.text_factory = str # 8-bit bytestrings
cur = conn.cursor()
# get subscribers and write to subscriber_list, no problems here
cur.execute('SELECT subscriber_id, phone_number FROM subscriber')
subscriber_list = cur.fetchall() # this is getting all the subscribers
# no problems here
# read values from tab-delimited csv file
i = 1 # do not use first line (contains headers)
reader = csv.reader(open(csvfile, "rU"), delimiter = '\t')
for Number, Name, Message, Datetime, Type in reader:
# check to ensure name/number not null and not first record
if Number and Name and i > 1:
# if number starts with '1' then remove first digit
if str(Number)[0] == '1':
Number = int(str(Number)[1:])
# this is where I think the problem is, but cannot figure out why
# return subscriber_id (sid) given phone number
for sid, num in subscriber_list:
if num == Number:
# insert messages into textmsg table
cur.execute('INSERT OR IGNORE INTO textmsg (subscriber_id, msg, datetime, type) VALUES (?,?,?,?)', (sid, Message, Datetime, Type))
conn.commit()
i += 1 # iterator to ensure first line is not used but others are
cur.close()
conn.close()
print '...Successfully populated textmsg table.'
This works for all the long numbers, but it does not get the data with the short numbers. Why?