![](/img/trans.png)
[英]How to write a function that runs that function on 20 different csv files in Python?
[英]Python: Creating a function to write .CSV files
我想将多个语料库的词频列表保存为 .CSV。 有没有办法让 Python 根据变量名自动写入文件名? (例如:corpus_a > corpus_a_typefrequency.csv)
我有以下代码,它已经适用于个人语料库:
from collections import Counter
import csv
counts = Counter(corpus_a)
counts = dict(sorted(counts.items(), key=lambda item: item[1],reverse=True))
with open('corpus_a_typefrequency.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in counts.items():
writer.writerow([key, value])
PS:如果我只能计算单词(没有标点符号)并且不区分大小写,那就太好了。 我还没有想出如何在这里做到这一点。 我正在使用来自布朗语料库的数据如下:
import nltk
from nltk.corpus import brown
corpus_a = brown.words()
我试过brown.words().lower().isalpha()
,但这不起作用。
你应该看看这个答案: https : //stackoverflow.com/a/40536047/5289234 。 它将允许您提取变量名称并使用它来保存 csv。
import inspect
def retrieve_name(var):
"""
Gets the name of var. Does it from the out most frame inner-wards.
:param var: variable to get name from.
:return: string
"""
for fi in reversed(inspect.stack()):
names = [var_name for var_name, var_val in fi.frame.f_locals.items() if var_val is var]
if len(names) > 0:
return names[0]
from collections import Counter
import csv
counts = Counter(corpus_a)
counts = dict(sorted(counts.items(), key=lambda item: item[1],reverse=True))
with open(retrieve_name(corpus_a) +'_typefrequency.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
for key, value in counts.items():
writer.writerow([key, value])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.