How To Write A List Of List Into Excel Using Python?
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, linetermin
Solution 1:
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:
Solution 2:
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, datain enumerate(new_list):
worksheet.write_row(row_num, 0, data)
Output:
Solution 3:
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.
Solution 4:
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")
Solution 5:
I think the most convenient way is to use Series in Pandas.
import pandas as pdnew_list=['whatever']
pd.Series(new_list)
new_list.to_excel('aFileName.xlsx')
Post a Comment for "How To Write A List Of List Into Excel Using Python?"