首页 > 解决方案 > 如何在 CSV 文件中写入单词及其向量?

问题描述

这是我想要的 CSV 输出:

word  \t vector
code  \t  0.85

这是我所做的:

for word in total_words[0]:
    with open("test.csv", "w") as csv_file:   
        writer = csv.writer(csv_file, delimiter='\t')
        writer.writerow(['word', 'vectors'])
        writer.writerow([word, bert(word)])

这只给了我第一个单词及其向量,而不是我需要的每个单词,也没有列表。有什么帮助吗?

标签: pythoncsv

解决方案


您只为 for 循环使用了 total_words 的第一个单词。

尝试这个:

with open("test.csv", "w") as csv_file:   
    writer = csv.writer(csv_file, delimiter='\t')
    writer.writerow(['word', 'vectors'])
    for word in total_words:
        writer.writerow([word, bert(word)])

推荐阅读