How to insert an array into a variable?

1

Realizing that it has a lot of repetitive code I decided to automate it by creating a looping of reading the cells of my excel file.

#row cell fixa para o horario
rows = ts[1].rows
cells = rows[1].cells
#Segunda 7h40min - 8h30min
cell = cells[3]
t1cell13 = cell.paragraphs
t1cell13 = str(t1cell13[0].text.encode("utf-8"))
t1cell13 = unicode(t1cell13, "utf-8")
print t1cell13
w_sheet.write(8,10, t1cell13, style)
a1 = t1cell13

#terça 7h40min - 8h30min
cell = cells[4]
t1cell14 = cell.paragraphs
t1cell14 = str(t1cell14[0].text.encode("utf-8"))
t1cell14 = unicode(t1cell14, "utf-8")
w_sheet.write(9,10, t1cell14), style)
a2 = t1cell14

#Quarta 7h40min - 8h30min
cell = cells[5]
t1cell15 = cell.paragraphs
t1cell15 = str(t1cell15[0].text.encode('utf-8'))
t1cell15 = unicode(t1cell15, "utf-8")
w_sheet.write(t1cell15, style)
a3 = t1cell15

#quinta 7h40min - 8h30min
cell = cells[6]
t1cell16 = cell.paragraphs
print str(t1cell16[0].text.encode('utf-8'))
w_sheet.write(11,10, str(t1cell16[0].text), style)
a4 = str(t1cell16[0].text.encode('utf-8'))

#sexta 7h40min - 8h30min
cell = cells[7]
t1cell17 = cell.paragraphs
print str(t1cell17[0].text.encode('utf-8'))
w_sheet.write(12,10, str(t1cell17[0].text), style)
a5 = str(t1cell17[0].text.encode('utf-8'))

#Sabado 7h40min - 8h30min
cell = cells[8]
t1cell18 = cell.paragraphs
a6 = str(t1cell18[0].text.encode('utf-8'))

Doing so:

#row cell fixa para o horario
rows = ts[1].rows
cells = rows[1].cells

i = 3
j = 8
k = 1

while (i < 9):
    cell = cells[(i)]
    item = cell.paragraphs
    item = str(item[0].text.encode("utf-8"))
    item = unicode(item, "utf-8")
    i = i+1

    while (j < 13):
        w_sheet.write((j),10, item, style)
        j = j+1

        while (k < 7):
            a(k) = item
            k = k+1

But in the final part of the while, where I try to put the (k), it is not working, I needed every lopping of the while to return: a1 = item a2 = item . . .

If you have another solution option you can also share.

Thank you very much.

    
asked by anonymous 01.04.2015 / 01:50

2 answers

2

There are some problems.

For example:

while (i < 9):

These parentheses are not required.

These also do not:

cell = cells[(i)]

I do not understand what this means:

a(k) = item

I think a is something like a dictionary, so in fact it would look like this:

a[k] = item

The final code looks like this:

#row cell fixa para o horario
rows = ts[1].rows
cells = rows[1].cells
a = {}

i = 3
j = 8
k = 1

while i < 9:
    cell = cells[i]
    item = cell.paragraphs
    item = str(item[0].text.encode("utf-8"))
    item = unicode(item, "utf-8")
    i += 1

    while j < 13:
        w_sheet.write(j, 10, item, style)
        j += 1

        while k < 7:
            a[k] = item
            k = k+1
    
01.04.2015 / 01:57
0

It worked right here ... I changed a little, anyway thank you = D

#row cell fixa para o horario
rows = ts[1].rows
cells = rows[1].cells

    a = {}

    i = 3
    j = 8
    k = 1

    while k < 7:
        cell = cells[k+2]
        item = cell.paragraphs
        item = str(item[0].text.encode("utf-8"))
        item = unicode(item, "utf-8")
        w_sheet.write(k+7, 10, item, style)
        a[k] = item
        k += 1

Placing in an array like this:

manhasegunda = (a[1], a[2], a[3], a[4], a[5], a[6])
    
01.04.2015 / 03:34