2

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 2

2

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:

excel output

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

Comments

1

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:

enter image description here


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)

Comments

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.