2

I have a list l1=[['a','20,'30],['b','30','40']. I want l1 to be inserted in an Excel file with this format:

a   20   30
b   30   40 
2
  • If I'm not wrong, you want to insert alphabets in one cell and numbers in another cell? something like this- | a | 20,30 | ? Commented Mar 12, 2020 at 8:17
  • Yes you are right Commented Mar 12, 2020 at 8:22

3 Answers 3

1

Using worksheet.write_column() with xlsxwriter

>>> import xlsxwriter
>>> a = [['a','20','30'],['b','30','40']]
>>> cl1 = [i[0] for i in a]                 # ['a', 'b']
>>> cl2 = [','.join(i[1:]) for i in a]      # ['20,30', '30,40']

>>> wbook = xlsxwriter.Workbook('Test.xlsx')
>>> wsheet = wbook.add_worksheet('Test')

>>> wsheet.write_column(0,0, cl1)
>>> wsheet.write_column(0,1, cl2)
>>> wbook.close()

Or

You can use pandas pandas.DataFrame.to_excel

>>> import pandas as pd
>>> df = pd.DataFrame.from_dict({'Column1':cl1,'Column2':cl2})
>>> df
  Column1 Column2
0       a   20,30
1       b   30,40

>>> df.to_excel('a_name.xlsx', header=True, index=False)
Sign up to request clarification or add additional context in comments.

Comments

0

You can try:

import pandas as pd
l1 = [['a','20', '30'], ['b','30', '40']]
df = pd.DataFrame({'col1': [l[0] for l in l1],
                   'col2': [l[1:3] for l in l1]})
df.to_excel('output.xlsx')

Comments

0

Try this:

import openpyxl
l1 = [['a','20','30'],['b','30','40']]
wb = openpyxl.Workbook()
sheet = wb.active
le_ = len(l1)
for i in l1:
    c1 = sheet.cell(row=1,column=1)
    c1.value = i[0]
    c2 = sheet.cell(row=1,column=2)
    c2.value = ' '.join(each for each in i[1:])
wb.save("demo1.xlsx")

In sheet.cell(row=1,column=1) you can point to specific cell using row number and column number and can store data in what ever format you want

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.