简体   繁体   English

唯一键和Python字典

[英]Unique Keys and Python Dictionary

Here is the code for the relevant function: 以下是相关功能的代码:

def populateSubscribers(csvfile, db):
  conn = sqlite3.connect(db)
  conn.text_factory = str  #bugger 8-bit bytestrings
  cur = conn.cursor()

  subscriber_dict = {}

  # read values from tab-delimited csv file
  reader = csv.reader(open(csvfile, "rU"), delimiter = '\t')
  for Number, Name, Message, Datetime, Type in reader:
    if str(Number)[:1] == '1':
      tmpNumber = str(Number)[1:]
      Number = int(tmpNumber)

      # check to ensure name/number not null
      if Number and Name:
        # add unique subscribers to dictionary
        subscriber_dict[Number] = Name
      else:
        print 'Subscriber missing name or number'

  # insert unique subscribers into subscriber table
  for number, name in subscriber_dict.items():
    cur.execute('INSERT OR IGNORE INTO subscriber (name, phone_number) VALUES (?,?)', (name, number))
    conn.commit()

  cur.close()
  conn.close()
  print '...Successfully populated subcriber table.'

It is reading subscriber names and phone numbers from a csv file and then it is supposed to write an entry for each unique subscriber into the database. 它正在从csv文件中读取订户名称和电话号码,然后应该将每个唯一订户的条目写入数据库。 I wanted the phone number to be the key in key/value pairs since it is unique. 我希望电话号码是键/值对中的键,因为它是唯一的。 But for some reason, it is not reading all of the numbers from the data, it is missing some subscribers. 但是由于某种原因,它没有从数据中读取所有数字,因此缺少一些订户。 If I make the name the key it misses as would be anticipated (de-duplicates all the Unknowns), but the phone number as key is missing some numbers altogether. 如果我将名称指定为密钥,那么它会像预期的那样丢失(对所有未知数进行重复数据删除),但是作为密钥的电话号码完全缺少一些数字。 Any ideas on how to fix the logic here? 关于如何解决这里的逻辑有什么想法?

Looks to me like if str(Number)[:1] == '1': is probably filtering out some of your data. 在我看来, if str(Number)[:1] == '1':可能正在过滤掉您的某些数据。

Add an else to that and print out any it's rejecting. 在其上添加一个else,并打印出所有拒绝的内容。 Those are probably the ones which are going wrong. 这些可能是出问题了。

Either way, pare down your input data and find which one's aren't being used. 无论哪种方式,请缩减您的输入数据并查找未使用的数据。 Without seeing the data and the alternative you've said which does work, it's hard to pin down the exact cause here. 如果没有看到相应的数据和你说这些工作的替代,很难在这里拖住确切原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM