我有一个包含多张工作表的Excel工作簿。我尝试迭代地使用Pandas read_excel()读取每张工作表的数据帧,为每张工作表输出单独的csv文件
def getSheets(inputfile, fileformat):
'''Split the sheets in the workbook into seperate CSV files in to folder
in the directory. CSV's are named identical to the original sheet names'''
name = getName(inputfile) # get name
try:
os.makedirs(name)
except:
pass
# read as df
df1 = pd.ExcelFile(inputfile)
# for each sheet create new file
for x in df1.sheet_names:
y = x.lower().replace("-", "_").replace(" ","_")
print(x + '.' + fileformat, 'Done!')
df2 = pd.read_excel(inputfile, sheet_name=x) #looking for way to dynamically find where the table begins
filename = os.path.join(name, y + '.' + fileformat)
if fileformat == 'csv':
df2.to_csv(filename, index=False)
else:
df2.to_excel(filename, index=False)
我遇到的问题是Excel工作簿有很多格式。结果是,实际表格从每张图纸的不同行开始。以下是工作簿中一张工作表的示例: example sheet
这里表格从第10行开始。在同一工作簿的其他工作表中,表格从第8行开始,依此类推。有>;50张,表格的第一行从不同的地方开始
我读过关于使用“skiprows”参数从特定行读取的方法。但是,对于我迭代的每一页,该参数的值都会发生变化。当每个表都从变量行开始时,有没有一种方法可以使用Pandas读取表中的数据,或者有没有一种方法可以确定表在Excel工作表中的实际开始位置
通过在调用
pd.read_excel
(或其近亲ExcelFile.parse
)之前手动读取Excel文件,可以找到表的起始位置:相关问题 更多 >
编程相关推荐