【问题标题】:Conceptual: writing a 2D matrix of dictionary results to a CSV file in Python概念:用 Python 将字典结果的 2D 矩阵写入 CSV 文件
【发布时间】:2016-12-11 17:25:00
【问题描述】:

我的字典格式是这样的:键是文档编号和关键字的元组,值是文档中关键字的频率。 因此,键将是(document1,keyword1),(document1,keyword2),(document1,keyword3),(document2,keyword1),(document2,keyword2),(document2,keyword3),(document3,keyword1),(document3 ,keyword2) 和 (document3,keyword3) 并且值将是数字。当然这是一本小字典。我希望将解决方案应用于大量文档和关键字。

字典是这样创建的:

document_count = {}
try:
    for doc in document_id_list:
        indiv_doc = # records selected from a database
        for w in words:
            document_count.setdefault((doc, w), 0)
            for entry in #unsorted list of text tokenized, set to lower case, and stripped of stop words:
                if entry == w and (doc, entry) in document_count:
                        document_count[(patent, entry)] += 1
    return document_count

except Exception, e:
    print "create claim storages"
    print str(e)
    pass

我想将结果写入 CSV,例如 2D 矩阵。至少,我看到它是这样描述的。

      keyword1 keyword2 keyword3
document1 number   number   number
document2 number   number   number 
document3 number   number   number

在查看 python.org 上的 CSV 函数文档和该站点上的其他问题时,我得到的最接近的是:

document1 keyword1 number
document1 keyword2 number
document1 keyword3 number
document2 keyword1 number
document2 keyword2 number
document2 keyword3 number
document3 keyword1 number
document3 keyword2 number
document3 keyword3 number 

这是我编写的代码的结果:

 with open(os.path.join('C:/Users/Tara/PyCharmProjects/untitled/csv_results/', file_name),
                    'wb') as csvfile:
   w = csv.writer(csvfile)
   for key, value in available_dict.items():
       separate_keys = list(key)
       w.writerow([separate_keys[0], separate_keys[1], value])

我注意到很多解决方案都涉及列表理解,但我不知道正确的 if 语句是什么。我会在编写字典或写入 CSV 文件时进行更改吗?

【问题讨论】:

    标签: python csv dictionary export-to-csv


    【解决方案1】:

    许多现有的 python 库处理编写 csv 文件的任务,所以我假设您只想使用简单的 python 语句和结构。

    下面的主要策略是编写一个生成器函数来创建 csv 文件的行。为此,该函数首先从字典中提取文档和关键字并对其进行排序,然后生成包含关键字的标题行,然后创建并生成每个文档的行

    我使用了最少数量的列表推导,如果您准备多写几行,可以轻松避免这种情况

    D = {
        ('doc1', 'key1'): 2, ('doc1', 'key2'): 3, ('doc1', 'key3'): 4,
        ('doc2', 'key1'): 4, ('doc2', 'key2'): 6, ('doc2', 'key3'): 8,
        ('doc3', 'key1'): 6, ('doc3', 'key2'): 9, ('doc3', 'key3'): 12,
    }
    
    def gen_rows(D):
        sorted_docs = sorted(set(t[0] for t in D))
        sorted_kwds = sorted(set(t[1] for t in D))
        yield [None,] + sorted_kwds
        for d in sorted_docs:
            yield [d,] + [D.get((d, k), 0) for k in sorted_kwds]
    
    for row in gen_rows(D):
        print(row)
    

    这是输出,准备写入 csv 文件的行列表

    [None, 'key1', 'key2', 'key3']
    ['doc1', 2, 3, 4]
    ['doc2', 4, 6, 8]
    ['doc3', 6, 9, 12]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-25
      • 2023-03-03
      • 2020-10-17
      • 2021-05-15
      相关资源
      最近更新 更多