Python-数据处理-数据读取02

一,解析Excel格式文件的数据。

使用xlrd库解析Excel格式文件,读取需要的数据。

从Table 9 中读取child_labor和child_marriage的数据,并通过pprint预打印出来。

首先创建一个空的数据结构,可以用来保存数据。

#-*- coding:utf-8 -*-
import xlrd
book = xlrd.open_workbook('SOWC 2014 Stat Tables_Table 9.xlsx')
sheet = book.sheet_by_name('Table 9 ')
data = {}
for i in xrange(14, sheet.nrows):
    #从第14行开始,因为这是国家数据的起点。
    row = sheet.row_values(i)
    country = row[1]
    data[country] = {
        'child_labor':{
            'total':[row[4],row[5]],
            'male':[row[6],row[7]],
            'female':[row[8],row[9]],
        },
        'child_marriage':{
            'married_by_15':[row[10],row[11]],
            'married_by_18':[row[12],row[13]],
        }
    }
    if country == 'Zimbabwe':
        break
import pprint
pprint.pprint(data)

猜你喜欢

转载自blog.csdn.net/dragon_a/article/details/80918748