Python3 ค้นหาค่าจากแผ่นงาน Excel

ฉันมีไฟล์ 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 และบันทึกผลลัพธ์ในพจนานุกรมได้อย่างไร ดูเหมือนเป็นงานที่ค่อนข้างง่าย แต่ฉันไม่สามารถเข้าใจและค้นหาตัวอย่างได้ ความช่วยเหลือใด ๆ จะได้รับการชื่นชมอย่างมาก


person tim_frost    schedule 27.08.2017    source แหล่งที่มา
comment
ลอง openpyxl.readthedocs.io/en/default   -  person Alex Hall    schedule 27.08.2017


คำตอบ (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” อันไหนต่ำสุด?

และฉันจะเสริมโค้ด ...

person Yutao ZHU    schedule 27.08.2017
comment
สำหรับออสเตรเลียขั้นต่ำ 3 สำหรับบริเตนใหญ่ 2 - person tim_frost; 27.08.2017
comment
คอลัมน์ C ของอุณหภูมิต่ำสุดหมายถึงอะไร เหมือน 1 เพื่ออะไร? ดังนั้นองค์ประกอบที่คุณต้องการได้คือ dict['Australia']['minimum temperature']['B'] = 3? - person Yutao ZHU; 27.08.2017
comment
นี่เป็นตัวอย่าง ฉันต้องการได้ผลลัพธ์ตามที่อธิบายไว้ข้างต้น - person tim_frost; 31.08.2017