python-3.x - 使用 XLRD 从 excel 表中的列中读取 int 值
问题描述
我在一个带有逗号分隔值的 Excel 工作簿中有一个单元格。
此单元格可以具有以下模式的值。
0
或123
或123, 345
。
XLRD
我想使用or将它们提取为整数列表pandas.read_excel
。
我尝试使用带有以下代码段的 xlrd。
book = open_workbook(args.path)
dep_cms = book.sheet_by_index(1)
for row_index in range(1, dep_cms.nrows)
excelList = []
excelList.extend([x.strip() for x in dep_cms.cell(row_index, 8).value.split(',')])
我什至尝试过熊猫
excel_frame = read_excel(args.path, sheet_name=2, skiprows=1, verbose=True, na_filter=False)
data_need = excel_frame['Dependent CMS IDS'].tolist()
print(data_need)
但得到的列表索引超出范围。
Reading sheet 2
Traceback (most recent call last):
File "ExcelCellCSVRead.py", line 25, in <module>
excel_frame = read_excel(args.path, sheet_name=2, skiprows=1, verbose=True, na_filter=False)
File "C:\Users\Kris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_base.py", line 311, in read_excel
return io.parse(
File "C:\Users\Kris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_base.py", line 868, in parse
return self._reader.parse(
File "C:\Users\Kris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_base.py", line 441, in parse
sheet = self.get_sheet_by_index(asheetname)
File "C:\Users\Kris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pandas\io\excel\_xlrd.py", line 46, in get_sheet_by_index
return self.book.sheet_by_index(index)
File "C:\Users\Kris\AppData\Local\Programs\Python\Python38-32\lib\site-packages\xlrd\book.py", line 466, in sheet_by_index
return self._sheet_list[sheetx] or self.get_sheet(sheetx)
IndexError: list index out of range
它不适用于单元格中的单个值(例如,只有 0 或某个值,如 123)。它正在输出AttributeError: 'float' object has no attribute 'split'
。
它仅在我有逗号分隔值时才有效,并将它们转换为字符串列表,如['123', '345']
. 我猜分裂条件是罪魁祸首。
如何使用 XLRD 或 pandas 将此单元格的值提取到整数列表中?
问候
解决方案
导入期间无法将逗号分隔值 (CSV) 与 Excel 进行比较。
而不是使用read_excel
你可以使用read_csv
.
以下是您的代码在应用后的样子的代码片段read_csv
Import Pandas as pd
df = pd.read_csv("your file name.csv")
data_need = df["Column_name"].tolist()
推荐阅读
- pandas - 如何对期间桶中的事件持续时间求和?
- excel - Excel VBA_ 对象工作表的方法范围失败(运行时错误 1004)
- python - 多处理与使用工人测功机
- android - 在片段中获取 null FragmentManager/Activity
- angular - 浏览器默认基本认证 spring-boot angular
- doxygen - 使用 doxyfile 配置在 Doxygen 中按字母顺序对图表进行排序
- c# - 为电子邮件上的后续文本设置自定义消息 - EWS c#
- javascript - “未捕获的 RangeError:超出最大调用堆栈大小”为什么会这样?
- sql - 当我需要从其他架构访问多个对象时,如何轻松安全地限制 SQL Server 中一个架构的用户?
- ios - 在 iTunes Connect - App Store 中更改版本