2015-04-03 2910 views
0

我沒有問題使用numpy導入csv數據,但不斷收到我的xlsx文件的錯誤。如何將xlsx文件轉換爲csv或如何將xlsx文件導入x2變量?Python如何使用numpy導入xlsx文件

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

方法的名字是loadtxt,而不是loadtext的只是一列。這解釋了您報告的錯誤。

但是,loadtxt將無法​​讀取OpenXML .xlsx文件。 .xlsx文件是一種二進制格式,並且相當複雜。您將需要使用專用於讀取此類文件的模塊,以便能夠讀取.xlsx文件。例如,xlrdopenpyxl都可以讀取.xlsx文件。

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

+0

[openpyxl](https://openpyxl.readthedocs.org/en/latest/)也可以使用,你可以使用[PyPI](https://pypi.python.org/pypi/openpyxl) 'pip install openpyxl' – 2015-04-03 20:01:31

3

NumPy沒有任何命令來讀取Excel文檔。對於xls和xlsx,使用openpyxl代替OpenXML(Excel >= 2007)或xlrd,因爲@David Heffernan暗示。您可以使用pip進行安裝。從openpyxl documentation例如:在reading Excel in Python

>>> 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

+1

[Pandas'read_excel()'](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html)也可以工作,並創建一個數據框,然後你就可以[使用'to_records'](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_records.html)將NumPy記錄數組轉換爲NumPy記錄數組,該數組保存dtypes或ND數組['as_matrix'](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.as_matrix.html)或['values'](https://pandas.pydata.org/pandas -docs /穩定/生成/ pandas.DataFrame.values.html)。 – 2017-08-07 17:02:25

+1

_EG_:'import pandas as pd; df = pd.read_excel('First_Persons_PT.xlsx'); x2 = df.to_records()' – 2017-08-07 17:03:16