Jayadevan
Jayadevan

Reputation: 1332

How can I print a Python list of arrays to excel

I have a python list with arrays like this -

['5a0aeaeea6bc7239cc21ee35', 'Salt & Sugar', 3701, 4172, -471]
['5a0aeaeea6bc7239cc21ee36', 'Atta & Flours', 2030, 2227, -197]
['5a0aeaeea6bc7239cc21ee37', 'Soya Products', 165, 185, -20]

How can I print this to excel so that the '[' and ']' are eliminated and the commas in the data don't cause an issue?

Upvotes: 0

Views: 48

Answers (2)

Sharan
Sharan

Reputation: 731

If its a list of lists then

import csv

l = [['5a0aeaeea6bc7239cc21ee35', 'Salt & Sugar', 3701, 4172, -471],
['5a0aeaeea6bc7239cc21ee36', 'Atta & Flours', 2030, 2227, -197],
['5a0aeaeea6bc7239cc21ee37', 'Soya Products', 165, 185, -20]]

with open("out.csv", "w", newline="") as f:
    writer = csv.writer(f)
    writer.writerows(l)

Upvotes: 0

Khairyi S
Khairyi S

Reputation: 306

You could try with pandas like this:

import pandas as pd
df = pd.DataFrame(your_list) 
df.to_excel('list.xlsx', index=False)

Upvotes: 1

Related Questions