20

How to write a list of list into excel using python 3?

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

I can write this list into csv file but How canI want to write in excel file?

1
  • 1
    Have a look at openpyxl or xlwings to write into a file with xl/xls format. But, you should be able to open your csv file in Excel right? Commented Apr 4, 2019 at 5:15

6 Answers 6

41

You can use the pandas library.

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

Related documentation:

Sign up to request clarification or add additional context in comments.

1 Comment

your answer creates the following error: raise ValueError("All arrays must be of the same length") ValueError: All arrays must be of the same length
21

Here is one way to do it using 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)

Output:

enter image description here

Comments

1

I think you should use pandas library to write and read data in this library the function pandas.DataFrame.to_excel(..) will make you able to directly write to excel files for all this you may need to define pandas.DataFrame for this work here is a tutorial on pandas-dataframe by dataCamp.

Comments

1

You can also do this with openpyxl library.

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.

Comments

0
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")

Comments

-3

I think the most convenient way is to use Series in Pandas.

import pandas as pd

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

1 Comment

AttributeError: 'list' object has no attribute 'to_excel'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.