如何使用python计算csv文件中单词的出现次数?

时间:2019-04-11 00:04:00

标签: python list file append

因此,对于我的作业,我给了一个csv文件,应该打印该文件中的人数,每种电子邮件的数量(.jp,.uk等),并在其中打印姓氏显示多次的文件。每当我运行该程序时,它说每种电子邮件类型都为0。我究竟做错了什么?

我认为通过使用“如果文件中有“”,它将找到每个文件并将其添加到“ jp”。有什么建议吗?

inFile = open('mydata.csv', 'r')

i = 0
jp = 0
uk = 0
de = 0

for line in inFile:
    i = i + 1
````
if '.jp' in inFile:
    jp = jp + 1
````
if '.uk' in inFile:
    uk = uk + 1
````
if '.de' in inFile:
    de = de + 1

print('There are', i - 1,'people in this file,')
print('There are', jp,'emails that end in .jp')
print('There are', uk,'emails that end in .uk')
print('There are', de,'emails that end in .de')

inFile.close()
outFile.close()

1 个答案:

答案 0 :(得分:0)

您不会针对if条件遍历文件的内容。

for line in file:
   i = i + 1
   if '.jp' in line:
       ...
   if '.uk' in line:
       ...