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

Posted

技术标签:

【中文标题】读取 Excel 分段数据,转换,然后输出为数据库的原始格式【英文标题】:Read Excel sectioned data, transform, then output to raw format for database 【发布时间】:2019-06-03 17:56:43 【问题描述】:

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

Example

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

Target output Example

【问题讨论】:

欢迎来到 SO!请显示您目前使用的代码以及问题所在。 【参考方案1】:

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.

获得所有表格标题的索引后,您可以创建另一个表格阅读器函数来迭代特定行的数据框。

【讨论】:

以上是关于读取 Excel 分段数据,转换,然后输出为数据库的原始格式的主要内容,如果未能解决你的问题,请参考以下文章

java利用poi技术导入批量excel数据,并且分段存入数据库怎么解决

EXCEL VBA 将多列转换为多行,列之间有间隙

java excel转换prn

python读取excel数据转换成字典

如何将excel的数据输出到文本文件中并设定长

如何上传Excel直接读取里面的数据,不用鎒xcel文件后再读取