2017-04-13 42 views
1

這裏有一個Python的問題:處理Excel數據使用Pyexcel

你好,我正在做一個web應用程序,它從把它們變成從一個整數電子表格(.csv)接收數據。評估這些值,返回這些值並將該數據寫入表格的第4列,每行。正如你可以看到我的代碼:

import fileinput 
import csv 
import pyexcel as pe 
records = pe.iget_records(file_name="test.xlxs") 






cho = raw_input("\nStart Forecaster on file?:<1/0>") 

if cho == 1: 

    for record in records: 
    rem = record[i,0] 
    sold1 = record[i,1] 
    sold2 = record[i,2] 

    rem = int(rem) 
    sold1 = int(sold1) 
    sold2 = int(sold2) 
    result = forecast(rem,sold1,sold2) 
    record[i,4] = result 
    print "Forecast Complete! Please check the file!" 


else: 
    quit() 




def calculate(rem,sold1,sold2): 

    result = ((l+t)/2)*3 
    return result 





def forecast(rem,sold1,sold2): 

    if (rmn == 0 and sold1 == 0 and sold2 ==0): #All ZERO 
      return 15 
    elif (rmn == 0 and sold1 == 0 and sold2 < 10): #ALL FOR ONE PRODUCT VALUE 
      return sold2*3 
    elif (rmn == 0 and sold1 < 10 and sold2 ==0): 
      return sold1*3 
    elif (rmn < 10 and sold1 == 0 and sold2 == 0): 
      return rmn*3 
    #END FOR ONE PRODUCT VALUE 
    elif (rmn>= 10 and sold1>=10 and sold2>=10): 

      if((rmn/3)>=(sold1+10) or (rmn/3)>=(sold1+10)): 
       return 0 
      else: 
       return calculate(rmn,sold1,sold2)-rmn 
    elif (rmn<10 and sold1<10 and sold2<10): 
      return calculate(rmn,sold1,sold2) 
    elif (rmn == 0 and sold1>=10 and sold2>=10): 
      return calculate(rmn,sold1,sold2) 
    else: 
      return sold1 

...有沒有錯誤,但它沒有對csv文件有任何影響。有任何想法嗎?也在print "Forecast Complete! Please check the file!" ..當我運行程序時,它不會到達那裏,這意味着循環必須有錯誤嗎?我正在搞清楚。但我也想要求幫助。

原始文件:

1 2 3 
1 2 3 
1 2 3 

我想做的發生:

1 2 3 result(digits) 
1 2 3 result(digits) 
1 2 3 result(digits) 
+0

我沒有特別使用這個庫,但它看起來並不像你在任何地方向這個文件寫入任何東西。 'records = pe.iget_records(file_name =「test.xlxs」)'只是將文件內容拖入Python,在那裏操縱它,然後丟棄結果。在編寫/保存更改時,請查看[教程](https://pyexcel.readthedocs.io/en/latest/tutorial_file.html#update-an-existing-column-to-an-existing-file)。 – roganjosh

+0

我試過'records.save_as(「test.xlsx」)'但它仍然不起作用,你怎麼看我的循環?它有什麼問題嗎? – dekt

+0

我現在沒有時間玩圖書館,也沒有測試數據。但是,在保存之前,您應該用數據的「打印」來驗證您實際計算了您認爲有的事情。 – roganjosh

回答

1

簡短的回答

pyexcel.iget_records返回字典的列表,並適用於數據一個標題行。 'records.save_as'將不起作用,因爲返回的數據結構是一個標準的Python列表,當然它不具有save_as功能。

pyexcel.Sheet實例將具有save_as函數,但應在代碼中使用'pyexcel.get_sheet'。或pyexcel.save_as,模塊級功能可以將數組保存到文件中。請參閱the example here

樣品溶液

>>> import pyexcel as p 
>>> sheet = p.get_sheet(file_name='test.xlsx') # pip install pyexcel-xlsx 
>>> sheet 
Sheet 1: 
+---+---+---+ 
| 1 | 2 | 3 | 
+---+---+---+ 
| 1 | 2 | 3 | 
+---+---+---+ 
| 1 | 2 | 3 | 
+---+---+---+ 
>>> for row in sheet: 
...  print row 
... 
[1, 2, 3] 
[1, 2, 3] 
[1, 2, 3] 
>>> results = [] 
>>> for row in sheet: 
...  results.append(sum(row)) # <- do your own forcast here 
... 
>>> results 
[6, 6, 6] 
>>> sheet.column += results 
>>> sheet 
Sheet 1: 
+---+---+---+---+ 
| 1 | 2 | 3 | 6 | 
+---+---+---+---+ 
| 1 | 2 | 3 | 6 | 
+---+---+---+---+ 
| 1 | 2 | 3 | 6 | 
+---+---+---+---+ 
>>> sheet.save_as('new.csv') 

龍答案

pyexcel可以幫助你在一個班輪獲得Python數據結構。有4 functions:get_array,get_dict,get_records和get_book_dict。提供了Two streaming functions來處理更大的數據大小:iget_array和iget_records。假定開發人員只需要數據進行分析。

pyexcel提供two custom functions幫助您處理數據:get_sheet和get_book。前者返回pyexcel.Sheet,後者返回pyexcel.Book。假設開發人員需要操作行,列和表格,然後將表格/書籍保存回Excel文件。

這樣說,然而,通用的Python數據結構可以很容易地存儲爲一個Excel文件。這裏是模塊級別的three save functions:save_as和save_book_as。以下是how to save an array into an xls file的示例。

與大熊貓相比,pyexcel是一款重量輕,易於安裝,易於理解和基於組件的Excel數據處理軟件包。然而,它並不是要取代大熊貓,而是爲數據分析任務提供一種替代方案,即不太複雜

與openpyxl和xlsxwriter,pyexcel讓您專注的不是文件格式的數據,在那裏你可以重用你的代碼來處理ODS,XLS和CSV文件格式無需更改代碼比較。

+0

chfw的另一個傳奇答案:) – ropata