0

I have a list of persons , each person is represented with array of strings:

person1=["amy","fisher",34,"teacher"]
person2=["john","wayne",45,"astronaut"]

I want to save the list of persons in an excel table and have as a header :

name family_name   age  profession
amy  fisher        34   teacher
2
  • so, what have you tried? Commented Apr 24, 2017 at 11:58
  • I managed to get another example work with numpy but in that example I used only integers .I can show you the code if you want. Commented Apr 24, 2017 at 12:04

2 Answers 2

2
import pandas as pd
person1=["amy","fisher",34,"teacher"]
person2=["john","wayne",45,"astronaut"]
#construct a pandas dataframe
df = pd.DataFrame(columns=['name','family_name','age','profession'], data=[person1,person2])
#write it to an excel file
df.to_excel('output.xls',index=False)
Sign up to request clarification or add additional context in comments.

11 Comments

But ,if I have 1000 persons, how should i modify the code?
How are your 1000 persons stored? Are they in a list or a file something else?
actually they are in a list
what's the format of the list? Does it look like this [["amy","fisher",34,"teacher"],["john","wayne",45,"astronaut"]]?
Yes, it looks exactly like that
|
0

This can be done using xlwt library, you can install it using pip install xlwt.

import xlwt

headers = ['name', 'family_name', 'age', 'profession']
persons = [["amy", "fisher", 34, "teacher"],
           ["john", "wayne", 45, "astronaut"]]

# font style
style0 = xlwt.easyxf('font: name Times New Roman, color-index black, bold on')

wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')

# write headers
for index, value in enumerate(headers):
    ws.write(0, index, value, style0)

# write data
for row, person in enumerate(persons):
    for index, value in enumerate(person):
        ws.write(row+1, index, value)       

wb.save('persons.xls')

This will create the excel file 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.