我正在尝试通过csv.DictReader获取csv文件并将其转换为字典.执行此操作后,我想修改字典的其中一列,然后将数据写入tsv文件.我正在处理文本中的单词和单词频率.
我已经尝试使用dict.value()函数来获取字典值,但是我收到一条错误消息,说"AttributeError:DictReader实例没有属性"值""
以下是我的代码:
#calculate frequencies of each word in Jane Austen's "Pride and Prejudice"
import csv
#open file with words and counts for the book, and turn into dictionary
fob = open("P&P.csv", "r")
words = csv.DictReader(fob)
dict = words
#open a file to write the words and frequencies to
fob = open("AustenWords.tsv", "w")
#set total word count
wordcount = 120697
for row in words:
values = dict.values()
print values
Run Code Online (Sandbox Code Playgroud)
基本上,我有文本中每个单词的总数(即"a","1937"),我想找到有问题的单词使用的总字数的百分比(因此,对于"a",百分比将是1937/120697.)现在我的代码没有这样做的公式,但是我希望,一旦我获得每一行的值,就用一个字和一个字来写新行的文件.计算百分比.如果有人有更好的方式(或任何方式!)这样做,我将非常感谢任何输入.
谢谢