Python如何使用numpy导入xlsx文件

时间:2015-04-03 19:39:45

标签: python csv numpy xlsx

我使用numpy导入csv数据没有问题,但是我的xlsx文件仍然出错。如何将xlsx文件转换为csv或如何将xlsx文件导入x2变量?

from matplotlib import pyplot as pp
import numpy as np

#this creates a line graph comparing flight arrival time, arrival in queue, and processing time

x,y = np.loadtxt ('LAX_flights.csv',
                unpack = True,
                usecols = (1,2),
                delimiter = ',')

print("Imported data set arrival time")

x2 = np.loadtext ('First_Persons_PT.xlsx',
               unpack = True,
               usecols=(0))

print ("Imported start of processing time")


#y2=
#print ("Imported final time when processed")

pp.plot(x,y, 'g', linewidth = 1)
#pp.plot(x2,y, 'y', linewidth = 1)
pp.grid(b=True, which = 'major', color='0', linestyle='-')

pp.title('Comparing Time of Arrival vs. Queue Arrival Time, Queue Finish Time')
pp.ylabel('Arrival in queue (Green),Process Time (Yellow)')
pp.xlabel('Time of arrival')

pp.savefig('line_graph_comparison.png')

这是错误

Imported data set arrival time
Traceback (most recent call last):
  File "C:\Users\fkrueg1\Dropbox\forest_python_test\Graph_time_of_arrival.py", line 13, in <module>
    x2 = np.loadtext ('First_Persons_PT.xlsx',
AttributeError: 'module' object has no attribute 'loadtext'

xlsx只是一个包含约100个数字的列

3 个答案:

答案 0 :(得分:5)

import pandas as pd
WS = pd.read_excel('ur.xlsx')
WS_np = np.array(WS)

使用pandas更简单

答案 1 :(得分:4)

方法的名称为loadtxt,而不是loadtext。这解释了您报告的错误。

但是,loadtxt将无法读取OpenXML .xlsx文件。 .xlsx文件是二进制格式,而且相当复杂。您将需要使用专用于读取此类文件的模块,以便能够读取.xlsx文件。例如,xlrdopenpyxl都可以读取.xlsx文件。

根据您的要求,提供文本文件而不是.xlsx文件可能更容易。

答案 2 :(得分:3)

NumPy没有任何读取Excel文档的命令。而是将openpyxl用于OpenXML(Excel >= 2007)或xlrd用于xls和xlsx,如@David Heffernan所示。您可以使用pip安装。来自openpyxl documentation示例:

>>> from openpyxl import load_workbook
>>> wb = load_workbook('First_Persons_PT.xlsx', read_only=True)
>>> print wb.sheetnames
['Sheet1', 'Sheet2', 'Sheet3']
>>> ws = wb.get_sheet_by_name('Sheet1')
>>> use_col = 0  # column index from each row to get value of
>>> x2 = np.array([r[use_col].value for r in ws.iter_rows()])
See my posts上的

reading Excel in Python