Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XRD python包解析.xlsx表_Python_Excel_Xlsx_Xlrd - Fatal编程技术网

使用XRD python包解析.xlsx表

使用XRD python包解析.xlsx表,python,excel,xlsx,xlrd,Python,Excel,Xlsx,Xlrd,我有一个xlsx表,其中包含如下所示的示例数据 fileinfo: sample file | DateCreated: 09/23/2013 | Country: Japan Num. | Name | Age | Sex | Country 1 | Mark | 45 | M | Australia 2 | Steve| 50 | M | United kingdom 3 | Julia| 35 | F | USA fileinfo: sample

我有一个xlsx表,其中包含如下所示的示例数据

fileinfo: sample file | DateCreated: 09/23/2013 | Country: Japan

Num. | Name | Age | Sex | Country

  1  | Mark | 45  | M   | Australia
  2  | Steve| 50  | M   | United kingdom
  3  | Julia| 35  | F   | USA

fileinfo: sample file | DateCreated: 09/23/2013 | Country: Japan

Num. | Name | Age | Sex | Country

  1  | Ronald | 64  | M   | USA
  2  | Micheal| 52  | M   | China
  3  | Zed    | 35  | F   | USA
该文件包含文件中任意位置的fileinfo行以及文件中的次数。我需要确保我没有在代码中捕获它们

在xlrd中是否有一种方法可以将标题和数据捕获到字典中,而不必考虑文本数据,在本例中,文本数据是文件信息行

这是我到现在为止的密码

import xlrd

def importXLSX(fileName):
    with xlrd.open_workbook(fileName) as wb:
        worksheet = wb.sheet_by_index(0)

    total_rows = worksheet.nrows

    num_rows, curr_row = worksheet.nrows, 0

    keys = [x.value for x in worksheet.row(1)]

    data = dict((x, []) for x in keys)

    while curr_row < num_rows:
        curr_row += 1
        for i, val in enumerate(worksheet.row(curr_row+1)):
            data[keys[i]].append(val)
    return data


data = importXLSX('simple.xlsx')
print data
导入xlrd
def importXLSX(文件名):
使用xlrd.open_工作簿(文件名)作为wb:
工作表=wb。工作表按索引(0)
总计行=工作表.nrows
行数,当前行=工作表.nrows,0
keys=[x.工作表中x的值。行(1)]
数据=dict((x,[])表示键中的x)
当前行<数量行时:
当前行+=1
对于i,枚举中的val(工作表行(curr_行+1)):
数据[键[i]]。追加(val)
返回数据
data=importXLSX('simple.xlsx')
打印数据

我终于找到了一种方法。当单元格为空时,XLRD返回零。因此,我能够比较带有空列的行,找出哪些是标题行,并将它们添加到列表中,最后迭代并打印它们