I have a Python list which has 6000 sub-lists. How do I make each of those sub-lists as a row in a single excel file? For example ls = [[1,2,3], [3,4,5]]. How do I make an excel file where first row is 1,2,3 and the second row is 3,4,5?
2 Answers
If you want to create an .xlsx file you can use xlsxwriter:
import xlsxwriter
workbook = xlsxwriter.Workbook('data.xlsx')
worksheet = workbook.add_worksheet()
ls = [[1,2,3], [3,4,5]]
for row_index, row in enumerate(ls):
for col_index, val in enumerate(row):
worksheet.write(row_index, col_index, val)
workbook.close()
Output:
Comments
You can create a CSV file from the list. For example:
import pandas as pd
ls = [[1, 2, 3], [3, 4, 5]]
df = pd.DataFrame(ls)
df.to_csv("data.csv", index=False, header=False)
Produces data.csv:
Or with built-in csv module:
import csv
ls = [[1, 2, 3], [3, 4, 5]]
with open("data.csv", "w") as f_out:
writer = csv.writer(f_out)
writer.writerows(ls)

