上面的这段优化报错print("行数:", len(data)) ^^^^^^^^^ TypeError: object of type 'int' has no len()
时间: 2023-11-13 07:41:14 浏览: 25
对不起,我在之前的代码中犯了一个错误。这是因为在优化后的代码中,`process_excel` 函数返回的是行数而不是数据本身。因此,在 `print_results` 函数中,我们应该将 `data` 参数更改为 `rows` 参数。修改后的代码如下:
```python
import pandas as pd
from openpyxl import load_workbook
def process_excel(file_path, output_file_path, sheet_name):
# 读取Excel文件
with pd.ExcelFile(file_path) as excel_file:
# 读取指定sheet页数据
data = pd.read_excel(excel_file, sheet_name=sheet_name)
# 合并第一列和第二列的数据,并放在第三列
data['交易场景'] = data['交易码'].astype(str) + data['交易名称'].astype(str)
# 保存修改后的数据到新的Excel文件
data.to_excel(output_file_path, sheet_name=sheet_name, index=False)
# 返回行数
return len(data)
def print_results(rows):
# 打印行数
print("行数:", rows)
# 打印结果
print(data['交易场景'][0])
# 获取所有sheet页名称
sheet_names = data['交易码'].values.tolist()
# 打印匹配的 sheet 页
matched_sheets = [sheet for sheet in excel_file.sheet_names if any(sheet_name in sheet for sheet_name in sheet_names)]
for sheet in matched_sheets:
print(sheet)
# 文件路径和输出文件路径
file_path = 'D:/workspace/python/7test.xlsx'
output_file_path = 'D:/workspace/python/6test.xlsx'
sheet_name = '客户信息系统交易接口交易总览'
# 处理 Excel 文件
rows = process_excel(file_path, output_file_path, sheet_name)
# 打印结果
print_results(rows)
```
现在应该不会再出现 `TypeError` 错误了。非常抱歉给你带来困扰,希望这次的修改能够解决问题!