这段代码哪里出错了import openpyxl src_path = u"D:\wula\副本中文金融情感词典_姜富伟等(2020).xlsx" dst_path = u"D:\wula\output.txt" workbook = openpyxl.load_workbook(src_path) sheetlist = workbook.sheetnames print(sheetlist) # 在这个列表里输入你想要处理的sheet页的名字 goal_sheet = ['negative', 'positive'] total_data = [] for item in sheetlist: if item in goal_sheet: column_num = workbook[item].max_column row_num = workbook[item].max_row for i in range(1, column_num+1): for j in range(1, row_num + 1):#不要表头把这里改成2 data = workbook[item].cell(row=j, column=i).value() total_data.append[data] with open(dst_path, 'w', encoding="utf-8") as f: for item in total_data: f.write(item + '\n')
时间: 2024-03-22 07:38:41 浏览: 119
4*4矩阵键盘与1602液晶显示
4星 · 用户满意度95%
这段代码有以下几个问题:
1. `src_path`和`dst_path`中的反斜杠`\`需要转义,否则会被识别为特殊字符,可以使用双反斜杠`\\`或者斜杠`/`来代替。
2. `workbook[item].cell(row=j, column=i).value()`应该是`workbook[item].cell(row=j, column=i).value`,因为`value`是属性而不是方法。
3. `total_data.append[data]`应该是`total_data.append(data)`,因为`append`是方法而不是属性。
修改后的代码如下:
```python
import openpyxl
src_path = u"D:/wula/副本中文金融情感词典_姜富伟等(2020).xlsx"
dst_path = u"D:/wula/output.txt"
workbook = openpyxl.load_workbook(src_path)
sheetlist = workbook.sheetnames
print(sheetlist)
# 在这个列表里输入你想要处理的sheet页的名字
goal_sheet = ['negative', 'positive']
total_data = []
for item in sheetlist:
if item in goal_sheet:
column_num = workbook[item].max_column
row_num = workbook[item].max_row
for i in range(1, column_num + 1):
for j in range(2, row_num + 1): # 不要表头把这里改成2
data = workbook[item].cell(row=j, column=i).value
total_data.append(data)
with open(dst_path, 'w', encoding="utf-8") as f:
for item in total_data:
f.write(str(item) + '\n')
```
修改后的代码中,使用了双反斜杠`\\`转义反斜杠`\`,将`value()`改为`value`,将`append[data]`改为`append(data)`,并且在写入文件时将元素转换成字符串类型。
阅读全文