import pandas
df = pandas.read_excel('path/to/data.xlsx') # The options of that method are quite neat; Stores to a pandas.DataFrame object
print df.head() # show a preview of the loaded data
idx_of_column = 5-1 # in case the column of interest is the 5th in Excel
print list(df.iloc[:,idx_of_column]) # access via index
print list(df.loc[['my_row_1','my_row_2'],['my_column_1','my_column_2']]) # access certain elements via row and column names
print list(df['my_column_1']) # straight forward access via column name
from xlrd import open_workbook
wb = open_workbook('simple.xls')
for s in wb.sheets():
print 'Sheet:',s.name
for row in range(s.nrows):
values = []
for col in range(s.ncols):
values.append(s.cell(row,col).value)
print ','.join(values)
我不确定您的数据是xlsx格式还是CSV格式。如果是XLSX,则使用this Python Excel tutorial。如果是CSV,就容易多了,您可以按照下面的代码片段进行操作。如果不想使用pandas,可以使用
numpy
库。使用下面的示例代码段获取CSV文件的顶行:这对于只有一列文本的文件有效。如果有更多的列,请使用下面的代码片段获取第一列。“0”表示第一列。
尽管pandas是一个很好的库,但对于简单的任务,您只需使用xlrd:
注意,
list
不是Python中变量的好名字,因为这是内置函数的名字。我建议安装熊猫。
以及
(签出pandas doc) 或者
代码
(来自https://github.com/python-excel/tutorial/raw/master/python-excel.pdf的示例)
相关问题 更多 >
编程相关推荐