我有一个带有示例数据的 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
该文件包含文件中任何位置的文件信息行以及文件中的次数。我需要确保我没有在我的代码中捕获它们。
在 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