0

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?

Dan
  • 4,488
  • 5
  • 48
  • 75
  • Might be somehow related, but could not spot "is" in your code http://stackoverflow.com/a/133024/315168 – Mikko Ohtamaa Jun 14 '12 at 03:53
  • 1
    It should definitely be `==` because they are comparing values, they are not the same object (one is in a list which came from a database table and the other comes from a csv file). – Dan Jun 14 '12 at 03:56
  • I tested it, it doesn't work at all with `is` in lieu of `==` – Dan Jun 14 '12 at 03:58
  • 1
    can you try `int(num) == int(Number)` – Samy Vilar Jun 14 '12 at 04:01
  • That solved it @samy.vilar thanks! Somehow it must have been reading the short numbers as strings instead of integers. Weird. Post it as an answer and I'll vote for it. – Dan Jun 14 '12 at 04:04

1 Answers1

1

Simply replace if num == Number: with if num == int(Number): and you should be good, you were already converting some of them, those that began with 1 but not others, the csv reader only returns strings while sqlite returns different types depending on the column ... and hence you were comparing string and ints ...

Samy Vilar
  • 10,800
  • 2
  • 39
  • 34
  • Thanks! Now that I see why it is not working I resolved it earlier in the code. – Dan Jun 14 '12 at 04:12