简体   繁体   English

Python:创建一个函数来编写 .CSV 文件

[英]Python: Creating a function to write .CSV files

I want to save word frequency lists as .CSV for several corpora.我想将多个语料库的词频列表保存为 .CSV。 Is there a way to make Python write the filenames automatically based on the variable name?有没有办法让 Python 根据变量名自动写入文件名? (eg: corpus_a > corpus_a_typefrequency.csv) (例如:corpus_a > corpus_a_typefrequency.csv)

I have the following code, which already works for individual corpora:我有以下代码,它已经适用于个人语料库:

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: it would be great if I could count only words (no punctuation) and also in a case-insensitive way. PS:如果我只能计算单词(没有标点符号)并且不区分大小写,那就太好了。 I haven't figured out how to do that here yet.我还没有想出如何在这里做到这一点。 I'm using data from the Brown Corpus as following:我正在使用来自布朗语料库的数据如下:

import nltk
from nltk.corpus import brown
corpus_a = brown.words()

I tried brown.words().lower().isalpha() , but that doesn't work.我试过brown.words().lower().isalpha() ,但这不起作用。

You should have a look at this answer: https://stackoverflow.com/a/40536047/5289234 .你应该看看这个答案: https : //stackoverflow.com/a/40536047/5289234 It will allow you to extract the variable name and use it to save the csv.它将允许您提取变量名称并使用它来保存 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM