Python列与CSV匹配

时间:2018-10-26 18:58:06

标签: python python-3.x csv

我正在尝试读取一个csv文件:base_list.csv-具有两列的CSV文件

然后读入file_1.csv并从base_list.csv文件中删除和匹配值,然后 将它们写到名为Dups.csv的新csv文件中

运行此命令时,出现以下错误:

  

emails = set(emails)#“ set”删除列表TypeError中的重复项:   不可散列的类型:“列表”

下面的示例代码:

import csv
#gather emails from base_list:
with open("H:\\Python Backups\\DeDup\\ByCSV\\base_list.csv", "rU") as base_file:
    read_base_file = csv.reader(base_file, delimiter=",")
    duplicates_list = []
    rows = [row for row in read_base_file]
    for row in rows:
        duplicates_list.extend(row)
    #extract emails from other csv files (csv_files) from multiple
    #columns in those csv files (email_columns):
    emails = []
    with open("H:\\Python Backups\\DeDup\\ByCSV\\file_1.csv", "rU") as csvfile:
        read_csv = csv.reader(csvfile, delimiter=",")     
        email_rows = [r for r in read_csv]
        emails.extend(email_rows)
    #find duplicates from base_list and remove them:
    duplicates = [e for e in emails if e in duplicates_list]
    for dupe in duplicates:
        emails.remove(dupe)
    emails = set(emails) #"set" removes duplicates in a list
    #write the emails to a csv:
    writer = csv.writer(open("H:\\Python Backups\\DeDup\\ByCSV\\Dups.csv", "ab"))
    for email in zip(emails):
        writer.writerow(email)

1 个答案:

答案 0 :(得分:0)

您遇到的错误是因为您试图将列表存储在集合中。这是不可能的,因为列表在python中是可变的,因此无法哈希。

>>> list_of_lists = [[1,2,3], ['a','b','c']]
>>> set(list_of_lists)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'

email_rows更改为

email_rows = (tuple(r) for r in read_csv)

这将创建一个元组生成器列表,现在可以将其哈希化。