3

As i'm trying to append my list values into one excel file as row wise i tried but it appending as column wise.

my INPUT is:

[[1 1 1 'Chair' 1 50 'hosur' Timestamp('2017-01-01 00:00:00')]
 [2 1 1 'Table' 1 50 'hosur' Timestamp('2017-01-02 00:00:00')]]

i want to append it into one excel file as ROW wise not COLUMN wise my tried code is

    import pandas as pd
    df = pd.DataFrame.from_dict({'row1':[[1 1 1 'Chair' 1 50 'hosur' Timestamp('2017-01-01 00:00:00')]
 ,'row2':[2 1 1 'Table' 1 50 'hosur' Timestamp('2017-01-02 00:00:00')]]})
    df.to_excel('test.xlsx', header=True, index=False)

1 Answer 1

2

Hope this helps You.. Try like this. Here I iterated the list within a list and appended the value.

expenses =     [[1, 1, 1, 'Chair', 1, 50, 'hosur', 'Timestamp(2017-01-01 00:00:00)'],
     [2,1, 1, 'Table', 1, 50, 'hosur','Timestamp(2017-01-02 00:00:00)']]

import openpyxl

wb = openpyxl.Workbook()

sheet = wb.active

le_ = len(expenses)
p = 0

for i in expenses:
     k = 0
     for j in i:
          c1 = sheet.cell(row=p+1,column=k+1)
          c1.value = str(j)
          k+=1
     p+=1

wb.save("demo1.xlsx")
Sign up to request clarification or add additional context in comments.

1 Comment

Its My pleasure @PriyaSRI

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.