Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (16.4k points)

I'm new to Python. I need to think of some information from my program to a spreadsheet. I've looked through on the web and there appear to be numerous packages accessible (xlwt, XlsXcessive, openpyxl). Others propose keeping in touch with a .csv document (never utilized CSV and don't actually comprehend what it is). 

The program is extremely straightforward. I have two lists (float) and three factors (strings). I don't have a clue about the lengths of the two lists and they presumably will not be a similar length.

I need a layout to be as in the image below:

The pink column will have the estimations of the main list and the green column will have the values of the subsequent list. 

So what's the most ideal approach to do this?

import xlwt

x=1

y=2

z=3

list1=[2.34,4.346,4.234]

book = xlwt.Workbook(encoding="utf-8")

sheet1 = book.add_sheet("Sheet 1")

sheet1.write(0, 0, "Display")

sheet1.write(1, 0, "Dominance")

sheet1.write(2, 0, "Test")

sheet1.write(0, 1, x)

sheet1.write(1, 1, y)

sheet1.write(2, 1, z)

sheet1.write(4, 0, "Stimulus Time")

sheet1.write(4, 1, "Reaction Time")

i=4

for n in list1:

    i = i+1

    sheet1.write(i, 0, n)

book.save("trial.xls")

How would I format the cells made in the for loop (list1 values) as logical or number?

1 Answer

0 votes
by (26.4k points)

Look at the below code:

import xlwt

def output(filename, sheet, list1, list2, x, y, z):

    book = xlwt.Workbook()

    sh = book.add_sheet(sheet)

    variables = [x, y, z]

    x_desc = 'Display'

    y_desc = 'Dominance'

    z_desc = 'Test'

    desc = [x_desc, y_desc, z_desc]

    col1_name = 'Stimulus Time'

    col2_name = 'Reaction Time'

    #You may need to group the variables together

    #for n, (v_desc, v) in enumerate(zip(desc, variables)):

    for n, v_desc, v in enumerate(zip(desc, variables)):

        sh.write(n, 0, v_desc)

        sh.write(n, 1, v)

    n+=1

    sh.write(n, 0, col1_name)

    sh.write(n, 1, col2_name)

    for m, e1 in enumerate(list1, n+1):

        sh.write(m, 0, e1)

    for m, e2 in enumerate(list2, n+1):

        sh.write(m, 1, e2)

    book.save(filename)

Want to become an expert in Python? Join the python course fast!

Related questions

0 votes
1 answer
0 votes
1 answer
asked Mar 18, 2021 in Data Science by dev_sk2311 (45k points)
0 votes
1 answer
asked Dec 10, 2020 in Python by laddulakshana (16.4k points)

Browse Categories

...