将计数器结果发送到 CSV,按字母分隔...只需要单词
Send Counter results to CSV, separating by letter...just want the word
我有一个 returns 个常用词列表 .txt
。我如何将它们发送到 CSV(或其他 Excel 文件),其中一列包含单词,另一列包含频率。
例如,这是我的 Counter(my_list)
返回值的开头:
Counter({'the': 3317, 'to': 1845, 'and': 1812, 'a': 1580, '': 1248, 'of': 1248, 'Harry': 1213, 'was': 1179, 'he': 1034, 'in': 932, 'his': 895, 'it': 803, 'said': 793, ...
我希望每个单词都在一列中,比如 A,计数在 B 中。喜欢
the | 3317
to | 1845
and | 1812
a | 1580
等(请注意,它可以在 CSV 中按字母顺序排序。我只是想把它放在那里进行分析)。
这是我现在拥有的:
def create_csv(my_list):
with open(r'myCSV.csv', 'w', newline='') as my_CSV:
fieldnames = ['word','count']
writer = csv.writer(my_CSV)
writer.writerow(fieldnames)
for key, value in my_list.items():
writer.writerow(list(key) + [value])
这 几乎 有效,除了每个 字母 在一列中,后跟计数:
我需要更改什么才能使单词保持在一起?
编辑:可以肯定的是,这是我用来创建列表的函数。 (my_file
是一个 .txt
文件)
def unique_words():
with open(my_file, encoding="utf8") as infile:
for line in infile:
words = line.split()
for word in words:
edited_word = clean_word(word)
lst.append(edited_word)
if edited_word not in lst:
lst.append(edited_word)
lst.sort()
return lst, cnt
并通过以下方式调用它:
create_csv(Counter(lst))
不要做 list(key)。直接放钥匙应该可以。
现在,假设单词在一行上并且 space 分开,
def Counter(my_file):
count = {}
with open(my_file, encoding="utf-8") as infile:
for line in infile:
words = line.strip().split()
for word in words:
#Assuming clean_word is a function to get rid of full stops, commas etc.
edited_word = clean_word(word)
count[edited_word] = count.get(edited_word, 0) + 1
return count
def create_csv(my_list):
with open(r'myCSV.csv', 'w', newline='') as my_CSV:
fieldnames = ['word','count']
writer = csv.writer(my_CSV)
writer.writerow(fieldnames)
for key, value in count.items():
writer.writerow([key, str(value)])
我有一个 returns 个常用词列表 .txt
。我如何将它们发送到 CSV(或其他 Excel 文件),其中一列包含单词,另一列包含频率。
例如,这是我的 Counter(my_list)
返回值的开头:
Counter({'the': 3317, 'to': 1845, 'and': 1812, 'a': 1580, '': 1248, 'of': 1248, 'Harry': 1213, 'was': 1179, 'he': 1034, 'in': 932, 'his': 895, 'it': 803, 'said': 793, ...
我希望每个单词都在一列中,比如 A,计数在 B 中。喜欢
the | 3317
to | 1845
and | 1812
a | 1580
等(请注意,它可以在 CSV 中按字母顺序排序。我只是想把它放在那里进行分析)。
这是我现在拥有的:
def create_csv(my_list):
with open(r'myCSV.csv', 'w', newline='') as my_CSV:
fieldnames = ['word','count']
writer = csv.writer(my_CSV)
writer.writerow(fieldnames)
for key, value in my_list.items():
writer.writerow(list(key) + [value])
这 几乎 有效,除了每个 字母 在一列中,后跟计数:
我需要更改什么才能使单词保持在一起?
编辑:可以肯定的是,这是我用来创建列表的函数。 (my_file
是一个 .txt
文件)
def unique_words():
with open(my_file, encoding="utf8") as infile:
for line in infile:
words = line.split()
for word in words:
edited_word = clean_word(word)
lst.append(edited_word)
if edited_word not in lst:
lst.append(edited_word)
lst.sort()
return lst, cnt
并通过以下方式调用它:
create_csv(Counter(lst))
不要做 list(key)。直接放钥匙应该可以。 现在,假设单词在一行上并且 space 分开,
def Counter(my_file):
count = {}
with open(my_file, encoding="utf-8") as infile:
for line in infile:
words = line.strip().split()
for word in words:
#Assuming clean_word is a function to get rid of full stops, commas etc.
edited_word = clean_word(word)
count[edited_word] = count.get(edited_word, 0) + 1
return count
def create_csv(my_list):
with open(r'myCSV.csv', 'w', newline='') as my_CSV:
fieldnames = ['word','count']
writer = csv.writer(my_CSV)
writer.writerow(fieldnames)
for key, value in count.items():
writer.writerow([key, str(value)])