gpt4 book ai didi

python - 读取Excel分段数据,转换,然后输出为数据库的原始格式

转载 作者:太空宇宙 更新时间:2023-11-03 21:22:53 24 4
gpt4 key购买 nike

我不知道这是否可能......还没有在网上遇到过这个。在 Excel 中,我已格式化按位置/城市划分的交叉表数据,所有这些数据都在同一个电子表格中,包含数千行。下面是简单的例子。

Example

我想运行一个 python excel 解析器,它接受此格式化数据并以原始数据格式取消格式化,以便我可以将其加载到数据库表中。这可能吗?期望的结果看起来像这样。

Target output Example

最佳答案

Pandas 有一种读取 Excel 文件的方法,这相当简洁,因为您可以从中获取数据框,这可能使扫描和自定义解析变得更容易。

import pandas as pd

# Reads the excel file
xl = pd.ExcelFile(file_path)
# Parses the desired sheet
df = xl.parse(sheet_name)

# To host all your table title indices
tbl_title = []

# To locate the title of your tables, I think you can do a sampling of that column to ascertain all the row numbers that contain the table titles
for i, n in enumerate(df.loc[:, column_name]):
if n == 'P': # The first column in your table header as the cue
tbl_title.append(i - 1) # This would be the row index for Frisco, Dallas etc.

一旦获得了所有表标题的索引,您就可以创建另一个表读取器函数来迭代特定行的数据帧。

关于python - 读取Excel分段数据,转换,然后输出为数据库的原始格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54101999/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com