【问题标题】:How to write a list of list into excel using python?如何使用python将列表列表写入excel?
【发布时间】:2022-04-26 02:50:49
【问题描述】:

如何使用python 3将列表写入excel?

new_list = [["first", "second"], ["third", "fourth"]]
with open("file_name.csv", 'w') as f:
    fc = csv.writer(f, lineterminator='\n')
fc.writerows(new_list)

我可以将此列表写入csv 文件,但我要如何写入 excel 文件?

【问题讨论】:

  • 查看openpyxlxlwings 以写入xl/xls 格式的文件。但是,你应该可以在 Excel 中打开你的 csv 文件吧?

标签: python excel python-3.x


【解决方案1】:

您可以使用pandas 库。

import pandas as pd

new_list = [["first", "second"], ["third", "four"], ["five", "six"]]
df = pd.DataFrame(new_list)
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
df.to_excel(writer, sheet_name='welcome', index=False)
writer.save()

相关文档:

【讨论】:

    【解决方案2】:

    这是使用XlsxWriter 的一种方法:

    import xlsxwriter
    
    new_list = [['first', 'second'], ['third', 'four'], [1, 2, 3, 4, 5, 6]]
    
    with xlsxwriter.Workbook('test.xlsx') as workbook:
        worksheet = workbook.add_worksheet()
    
        for row_num, data in enumerate(new_list):
            worksheet.write_row(row_num, 0, data)
    

    输出:

    【讨论】:

      【解决方案3】:

      我认为您应该使用pandas 库在此库中写入和读取数据,pandas.DataFrame.to_excel(..) 函数将使您能够直接写入 excel 文件,您可能需要在此处为​​这项工作定义 pandas.DataFrame是 dataCamp 在 pandas-dataframe 上的 tutorial

      【讨论】:

        【解决方案4】:

        您也可以使用 openpyxl 库来做到这一点。

        from openpyxl import Workbook
        
        new_list = [["first", "second"], ["third", "fourth"]]
        
        wb = Workbook() # creates a workbook object.
        ws = wb.active # creates a worksheet object.
        
        for row in new_list:
            ws.append(row) # adds values to cells, each list is a new row.
        
            
        wb.save('File_Name.xlsx') # save to excel file.
        

        【讨论】:

          【解决方案5】:
          import pyexcel
          
          # Get the data
          new_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
          
          # Save the array to a file
          pyexcel.save_as(array=new_list, dest_file_name="array_data.xls")
          
          
          # Retrieve the records of the file
          # records = pyexcel.get_records(file_name="test.xls")
          
          # Get an array from the data
          # my_array = pyexcel.get_array(file_name="test.xls")
          
          # Get your data in a dictionary of 2D arrays
          # 2d_array_dictionary = pyexcel.get_book_dict(file_name="test.xls")
          
          # The data
          # 2d_array_dictionary = {'Sheet 1': [
                                         ['ID', 'AGE', 'SCORE']
                                         [1, 22, 5],
                                         [2, 15, 6],
                                         [3, 28, 9]
                                        ],
                             'Sheet 2': [
                                          ['X', 'Y', 'Z'],
                                          [1, 2, 3],
                                          [4, 5, 6]
                                          [7, 8, 9]
                                        ],
                             'Sheet 3': [
                                          ['M', 'N', 'O', 'P'],
                                          [10, 11, 12, 13],
                                          [14, 15, 16, 17]
                                          [18, 19, 20, 21]
                                         ]}
          
            # Save the data to a file                        
            # pyexcel.save_book_as(bookdict=2d_array_dictionary, dest_file_name="2d_array_data.xls")
          

          【讨论】:

            【解决方案6】:

            我认为最方便的方法是在 Pandas 中使用 Series。

            import pandas as pd
            
            new_list =['whatever']
            pd.Series(new_list)
            new_list.to_excel('aFileName.xlsx')
            

            【讨论】:

              猜你喜欢
              • 2013-02-06
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-12-09
              相关资源
              最近更新 更多