How to insert an array into a variable?

Asked

Viewed 428 times

1

Realizing that it has a lot of repetitive code I decided to automate 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 at the end of the while where I try to put a(k), this not working needed every last while lopping me back: a1 = item a2 = item . . .

If you have another solution option you can also share.

Thank you very much.

2 answers

2

There are some little problems.

For example:

while (i < 9):

These parentheses are not required.

These also do not:

cell = cells[(i)]

I don’t understand what that means:

a(k) = item

I think that a is something like a dictionary, so it would actually be like this:

a[k] = item

The final code went 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
  • I think with a(k) = item he’s trying to create a dictionary anyway. That’s what he was going to answer as a solution! + 1

  • Thanks for the quick reply =D no a(k) I can’t tell you if it’s a dictionary. then I will need to use the following way manhasegunda = (a1, A7, A13, A19, A25, A31) so I have return that the a1 variable is not defined. Obs I had tried this way tbm =D

0

It worked out here... I changed a little, anyway thanks a lot =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

Putting in an array like this:

manhasegunda = (a[1], a[2], a[3], a[4], a[5], a[6])

Browser other questions tagged

You are not signed in. Login or sign up in order to post.