python - 如何在 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)])
这只给了我第一个单词及其向量,而不是我需要的每个单词,也没有列表。有什么帮助吗?
解决方案
您只为 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)])
推荐阅读
- python - 在数组的所有元素中应用布尔运算符
- permutation - 什么是排名上下文中的右不变性?
- git - `git stash create` 不返回哈希
- c++ - 为什么不能在 range-v3 for_each 中使用指向成员的指针作为可调用/可调用?
- sql - 尝试将 csv 文件导入 SQL,错误提示索引列表超出范围?如何解决?
- c++ - 使用指令不适用于 c++20 概念吗?
- python-3.x - 使用 Python 3.7 而不使用 pandas 聚合成字典列表的平均值、最小值和最大值
- python - 同时提取行数据并在 Pandas 中将其转换为列数据
- java - 将 JSON 解析为 JAVA 时出现问题
- android - 如何在 android studio 中使用 setText 而不是使用 append?