python - 如何按列名提取excel数据?

标签 python arrays excel python-2.7

我的 excel 数据如下所示:

    A    B    C
1   123  534  576
2   456  745  345
3   234  765  285

在另一个 Excel 电子表格中,我的数据可能如下所示:
    B    C    A
1   123  534  576
2   456  745  345
3   234  765  285

如何从两个电子表格中提取 C 列的内容?

我的代码如下:
#Open the workbook
ow = xlrd.open_workbook('export.xlsx').sheet_by_index(0)

#Store column 3's data inside an array
ips = ow.col_values(2, 1)

我想要更多类似的东西:ips = ow.col_values(C, 1)
我怎样才能实现上述目标?

最佳答案

由于我有两个不同的电子表格,并且我想要的数据位于两个单独的行中,因此我必须按名称搜索第一行,直到找到它,然后提取该列。

我是这样做的:

ow = xlrd.open_workbook('export.xlsx').sheet_by_index(0)

for x in range (0, 20):
    try:
        if ow.cell_value(0, x) == "IP Address":
            print "found it!"
            ips = ow.col_values(x, 1)
            break
    except IndexError:
        continue

关于python - 如何按列名提取excel数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43221330/

相关文章:

python - Python 中 set.discard 和 set.remove 方法之间的运行时差异?

excel - 动态设置用户窗体标签前景色

excel - 可以安排从 SQL Developer 到 Excel 的数据导出吗?

javascript - Google Apps 脚本 - 映射数组

javascript - 在 Javascript 中将字符串转换为二维数组的有效方法

java - 将数组中的元素移动到空位置

excel - ActiveCell.Offset() 和 Range() 之间的速度差异?

python - 在 Jupyter Notebook 上离线绘制 icreate_animations

python - 替代 Python 字符串替换方法

python - 如何导入已导入的具有相同文件名的python模块?