首页 > 解决方案 > 如何将 csv 数据添加到我的 Hashtable?Python

问题描述

我有一个带有插入函数的哈希表,我正在尝试使用它将我的 csv 中的数据添加到表中。

 def insert(self, key, value):  # does both insert and update
        # get the bucket list where this item will go.
        bucket = hash(key) % len(self.table)
        bucket_list = self.table[bucket]

        # update key if it is already in the bucket
        for kv in bucket_list:
            # print (key_value)
            if kv[0] == key:
                kv[1] = value
                return True

        # if not, insert the item to the end of the bucket list.
        key_value = [key, value]
        bucket_list.append(key_value)
        return True

hashtable 和 csv_reader 是项目中的单独文件。我可以看到 csv 阅读器正在使用 print(row) 输出。我可以使用插入函数调用 csv 文件中的哈希表类吗?我试图尝试 h.insert(key, value) 但是当我打印结果时它总是对我输入的任何键都说 none 尽管我可以看到数据被正确读取。希望我问的正确。感谢您的帮助!

with open('WGUPS.csv') as file:
package_data = csv.reader(file, delimiter=',')
for row in package_data:
    print(row)

标签: python

解决方案


推荐阅读