如何使用Python从xlsx文件加载数据
这是我的xlsx文件:
我想把这些数据转换成一个字典,像这样:
{
0:{
'a':1,
'b':100,
'c':2,
'd':10
},
1:{
'a':8,
'b':480,
'c':3,
'd':14
}
...
}
所以有没有人知道哪个Python库可以做到这一点,从第124行开始,到第141行结束,
谢谢
4 个回答
0
另一个选择是openpyxl。我一直想试试这个工具,但还没时间去做,所以我不能说它有多好。
1
假设你有这样的数据:
a,b,c,d
1,2,3,4
2,3,4,5
...
在2014年,有很多可能的答案之一是:
import pyexcel
r = pyexcel.SeriesReader("yourfile.xlsx")
# make a filter function
filter_func = lambda row_index: row_index < 124 or row_index > 141
# apply the filter on the reader
r.filter(pyexcel.filters.RowIndexFilter(filter_func))
# get the data
data = pyexcel.utils.to_records(r)
print data
现在这些数据变成了一个字典的数组:
[{
'a':1,
'b':100,
'c':2,
'd':10
},
{
'a':8,
'b':480,
'c':3,
'd':14
}...
]
你可以在这里查看相关文档
1
关于 xlrd 的选择:
(1) 你的 xlsx 文件看起来不大,可以把它保存为 xls 格式。
(2) 使用 xlrd
加上一个测试版的附加模块 xlsxrd
(可以找到我的邮箱地址来问我要);这两个结合起来可以无缝读取 xls 和 xlsx 文件(用的是相同的接口;它会检查文件内容来判断是 xls、xlsx 还是其他格式)。
无论哪种情况,下面这个(未经测试的)代码应该能满足你的需求:
from xlrd import open_workbook
from xlsxrd import open_workbook
# Choose one of the above
# These could be function args in real live code
column_map = {
# The numbers are zero-relative column indexes
'a': 1,
'b': 2,
'c': 4,
'd': 6,
}
first_row_index = 124 - 1
last_row_index = 141 - 1
file_path = 'your_file.xls'
# The action starts here
book = open_workbook(file_path)
sheet = book.sheet_by_index(0) # first worksheet
key0 = 0
result = {}
for row_index in xrange(first_row_index, last_row_index + 1):
d = {}
for key1, column_index in column_map.iteritems():
d[key1] = sheet.cell_value(row_index, column_index)
result[key0] = d
key0 += 1