TypeError:' set' object不支持excel

时间:2017-06-15 01:17:50

标签: python excel loops

有人能帮助我吗?我有这个错误。我想循环并将wordnet中的所有数据插入excel中的不同列。它将首先读取一个包含一列的excel文档。然后,它将创建其他excel并为每个单词生成行和不同列的同义词。

此代码在一列中生成所有同义词:

import nltk
import xlrd
import csv
import xlwt
import xlsxwriter
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.corpus import wordnet

workbook = xlrd.open_workbook('C:\\Users\\runeza\Desktop\database.xlsx')
sheet_names = workbook.sheet_names()
sheet = workbook.sheet_by_name(sheet_names[0])

wb = xlwt.Workbook()
ws = wb.add_sheet("test")
for col_idx in range(sheet.ncols):
    for row_idx in range(sheet.nrows):
        cell = sheet.cell(row_idx, col_idx).value #read content in column cell
        synonyms = []
        for syn in wordnet.synsets(cell):
            for l in syn.lemmas():
                #print(l.name())
                synonyms.append(l.name())
                a = set(synonyms)
        #print (a)
        ws.write(row_idx, col_idx,",".join(a))

wb.save("sample.xls")

我修改了代码以正确地将单词放在不同的列中:

import nltk
import xlrd
import csv
import xlwt
import xlsxwriter
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.corpus import wordnet

workbook = xlrd.open_workbook('C:\\Users\\runeza\Desktop\database.xlsx')
sheet_names = workbook.sheet_names()
sheet = workbook.sheet_by_name(sheet_names[0])

wb = xlwt.Workbook()
ws = wb.add_sheet("test")

for col_idx in range(sheet.ncols):
    for row_idx in range(sheet.nrows):
        cell = sheet.cell(row_idx, col_idx).value #read content in column cell
        synonyms = []

        for syn in wordnet.synsets(cell):
            for l in syn.lemmas():
                synonyms.append(l.name())
                a = set(synonyms)
        #print (a)

        for col_idx in range(len(a)):
            for row_idx in range(len(a[col_idx])):
                ws.write(col_idx, row_idx, a[col_idx][row_idx])

wb.save("sample.xls")

但它给出了这个错误:

Traceback (most recent call last):
  File "C:\Users\runeza\Documents\PythonCode\outputfile.py", line 29, in <module>
    for row_idx in range(len(a[col_idx])):
TypeError: 'set' object does not support indexing

这就是我的(C:\ Users \ runeza \ Desktop \ database.xlsx)外观:

existing sheet

这是我的预期结果:

enter image description here

1 个答案:

答案 0 :(得分:0)

根据您的需要,我认为list将是更好的选择。 set个对象不支持索引(如[0])。

假设要将同义词添加到右侧单元格,您可以执行以下操作:

for col_idx in range(sheet.ncols):
    for row_idx in range(sheet.nrows):
        cell = sheet.cell(row_idx, col_idx).value #read content in column cell
        synonyms = []

        for syn in wordnet.synsets(cell):
            for l in syn.lemmas():
                synonyms.append(l.name())

        # Making the list elements distinct.
        synonyms = list(set(synonyms))

        for i in range(len(a)):
            ws.write(row_idx, col_idx+i, synonyms[i]) # Made an edit here.

wb.save("sample.xls")