D:\360Downloads\PYCHARM\PYCAHRMP\code\.venv\Scripts\python.exe D:\360Downloads\PYCHARM\PYCAHRMP\code\kun\0523.py Traceback (most recent call last): File "D:\360Downloads\PYCHARM\PYCAHRMP\code\.venv\Lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc return self._engine.get_loc(casted_key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas\\_libs\\hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\\_libs\\hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: 'TextsToTranslate' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\360Downloads\PYCHARM\PYCAHRMP\code\kun\0523.py", line 38, in <module> translate_column(input_filepath, worksheet_title, target_column) File "D:\360Downloads\PYCHARM\PYCAHRMP\code\kun\0523.py", line 12, in translate_column original_texts = df[column_to_translate].tolist() ~~^^^^^^^^^^^^^^^^^^^^^ File "D:\360Downloads\PYCHARM\PYCAHRMP\code\.venv\Lib\site-packages\pandas\core\frame.py", line 4102, in __getitem__ indexer = self.columns.get_loc(key) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\360Downloads\PYCHARM\PYCAHRMP\code\.venv\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc raise KeyError(key) from err KeyError: 'TextsToTranslate' 进程已结束,退出代码为 1
时间: 2025-05-31 12:48:52 浏览: 16
### 解决Python Pandas中KeyError: 'TextsToTranslate'
当尝试访问Excel文件中的某一列时,如果发生 `KeyError: 'TextsToTranslate'` 错误,通常是因为所指定的列名不存在于DataFrame对象之中。这种情况可能是由以下几个原因引起的:
1. **列名拼写错误**
确认所提供的列名 `'TextsToTranslate'` 是否完全匹配实际存在于Excel文件内的列头名称。注意大小写敏感度以及任何多余的空格字符等问题[^1]。
2. **读取过程中丢失了列名**
如果在调用 `pd.read_excel()` 方法时不正确设置参数(例如未指明正确的sheet name),可能会导致某些预期之外的结果,比如默认将首行当作数据而非标题从而使得所有列都获得自动生成的名字如0, 1, 2...等等而不是原本应有的描述性标签[^2]。
3. **缺失值处理不当**
若目标列包含大量空白单元格甚至全为空白状态的话也可能引起类似的键查找失败现象。因此建议预先检查输入源的质量再决定如何应对这些特殊情况下的记录条目。
针对以上提到的各种可能性提供相应的解决方案如下所示:
#### 修改后的代码片段
```python
import pandas as pd
def safe_read_excel(file_path, sheet_name=0):
"""Safely read an Excel file into a DataFrame."""
try:
df = pd.read_excel(
io=file_path,
sheet_name=sheet_name,
header=0, # Assuming first row contains headers.
dtype=str # Treat everything initially as string to avoid type issues.
)
# Clean up column names by stripping whitespace and converting them to lowercase.
cleaned_columns = {col: col.strip().lower() for col in df.columns}
df.rename(columns=cleaned_columns, inplace=True)
return df
except Exception as e:
raise ValueError(f"Failed reading the Excel file due to error: {str(e)}")
# Example usage of function defined above.
input_filepath = './example.xlsx'
worksheet_title = 'Sheet1'
df = safe_read_excel(input_filepath, worksheet_title)
if 'textsTotranslate'.lower() not in df.columns:
raise KeyError("The specified column does not exist after cleaning.")
else:
print(df['textsTotranslate'])
```
在此修正版中增加了几个重要的改进措施:
- 设置了固定的header行为以确保不会意外忽略真正的头部信息;
- 强制转换全部内容为字符串类型防止因数值型字段而导致潜在冲突;
- 对所有的列进行了清理操作统一调整为了小写字母形式方便后续比较判断逻辑更加简洁清晰;
---
###
阅读全文