【问题标题】:Python3 finding value from excel sheetPython3从excel表中寻找价值
【发布时间】:2017-08-27 12:08:45
【问题描述】:

我有一个带有一个工作表的 Excel 文件

例子:

A                   B  C

Australia

minimum temperature 3  1

average temperature 6  5

Great Britain

minimum temperature 2  7

average temperature 9  2


result:

Australia - 3; Great Britain - 2

如何在B列中找到澳大利亚的最低温度,在C列中找到英国的平均温度并将结果记录在字典中? 这似乎是一个非常简单的任务,但我无法理解并找到示例。 任何帮助将不胜感激。

【问题讨论】:

标签: python excel python-3.x xlsx


【解决方案1】:

我更喜欢使用 xlrd 来阅读 excel。其他包装也是合适的。视情况而定。

import xlrd
data = xlrd.open_workbook('your excel file path')
table = data.sheets()[0] # assume that your data is in the 1st sheet
nrows = table.nrows # get the rows in the table
data_dict = {}
for i in range(nrows):
    if i == 0:
        countinue # ignore the first row 'A B C'
    row = table.row_values(i) # get the data in the ith row
    if (i - 1) % 3 == 0:
        data_dict[row[0]] = {} # a dict whose key is column A's value
        country_name = row[0]
    else:
        data_dict[country_name][row[0]] = {}
        data_dict[country_name][row[0]]['B'] = row[1]
        data_dict[country_name][row[0]]['C'] = row[2]

然后你可以得到像 data_dict['Australia']['minimum temperature']['B'] = 3 这样的结果

你能给我一个更清晰的格式吗? "最低温度 3 1" 哪一个是最低的?

我会补充代码...

【讨论】:

  • 澳大利亚最低 3,英国 2
  • 最低温度C列的含义是什么。像“1”是为了什么?所以你想要得到的元素是 dict['Australia']['minimum temperature']['B'] = 3?
  • 这是例如。我想得到如上所述的结果
猜你喜欢
  • 1970-01-01
  • 2022-07-05
  • 1970-01-01
  • 2016-06-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-10
相关资源
最近更新 更多