我在excel工作簿中有一个用逗号分隔值的单元格。
此单元格可以具有以下模式的值。
[0
或123
或123, 345
。
我想使用XLRD
或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()