使用python和xlrd计算excel文件中特定单词的出现次数

时间:2015-04-08 10:05:45

标签: python xlrd import-from-excel

我有这些excel表,我已经创建了一个python脚本,它使用xlrd来读取这些脚本,并将信息汇总到它生成的.txt文件中。作为摘要的一部分,我需要它来计算特定关键字的次数,例如" cloud"出现在这些excel文件中。问题是当我读取单元格并将字符串拆分成单个单词时,它会在这些单词中添加引号和\ n,以便计数器错过它们。这是我的代码。

for filename in os.listdir(path):
count+=1

if filename.find('xls') != -1:
    print filename        
    workbook=xlrd.open_workbook(filename)
    sheet=workbook.sheet_by_name("Details")
    values = []
    newvalues = []
    for row in range(sheet.nrows):
        for col in range(sheet.ncols):
            values.append(sheet.cell(row,col).value)
    newvalues = str(values).split();
    a=[item.replace("\n", "") for item in newvalues]
print a.count("cloud") 

任何建议都将不胜感激。

0 个答案:

没有答案