2016-04-29 1707 views
0

我想將字典寫入excel文件。我總是收到一些錯誤信息,有人能解釋我該怎麼辦?Python將字典寫入excel

import sys 
    import argparse 

    import pyexcel 
    import pyexcel.ext.xls 
    import os 

#reading input.. 

    dict = {'Kalibrierwerte': 
       [array_1] 
     , 
      'Messwerte': 
       [array_2] 
     } 

    book=pyexcel.get_book(adict=dict) 

    book.save_as("../data/output2.xls") 

if __name__ == '__main__': 
    sys.exit(main()) 

當我運行這段代碼,我得到這個錯誤:

'File "C:\Python27\lib\site-packages\pyexcel\sources_init_.py", line 102, in _get_book raise NotImplementedError(MESSAGE_ERROR_NO_HANDLER) NotImplementedError: No suitable plugins imported or installed' 

我嘗試不同的方法寫的輸出,但沒有一個成功的。

+1

見代碼有什麼錯誤? – orvi

+0

對於發佈的代碼:'文件'C:\ Python27 \ lib \ site-packages \ pyexcel \ sources \ __ init__.py「,第102行,在_get_book中 raise NotImplementedError(MESSAGE_ERROR_NO_HANDLER) NotImplementedError:沒有合適的插件導入或安裝' – Miguel13366

回答

0

我不熟悉pyexcel,但我認爲你可以用excel支持的csv格式保存數據。

這是低於例如:

import pandas as pd 

array_1 = ['foo', 'bar', 'i', 'j'] 
array_2 = [1, 2, 3, 4] 
your_dict = {  # do not use `dict` to name a variable 
    'Kalibrierwerte': array_1, 
    'Messwerte': array_2, 
} 

# convert `dict` to `pandas.DataFrame` and save to csv file 
d = pd.DataFrame(your_dict) 
d.to_csv('output.csv') 
+0

我需要不同的牀單,所以,我不想擁有一個csv。 (這裏有兩個以上的小陣列,每個陣列都是一張) – Miguel13366

+0

@ Miguel13366代碼中的關鍵字參數'adict'是什麼'book = pyexcel.get_book(adict = dict)'?我無法在[get_book的文檔]中找到它(http://pyexcel.readthedocs.io/en/latest/generated/pyexcel.get_book.html?highlight=get_book#pyexcel.get_book) – linusp

0

正是有了字典和getbook問題。下面

dict_out = {'Kalibrierwerte': array_1, 
      'Messwerte':array_2 
      } 

book=pyexcel.get_book(bookdict=dict_out) 
book.save_as("../out/output.xls") 
+0

您可以使用一個liner:pyexcel.save_book_as(bookdict = dict_out,dest_file_name =「../out/output.xls」) – chfw