How to print to the console 2 columns side by side?

75 views
Skip to first unread message

Marco Goldin

unread,
Dec 20, 2015, 5:52:37 AM12/20/15
to openpyxl-users
Hi, i'm really new to openpyxl but i'm trying to get all values from 2 columns and printing them side by side.

 tuple(sheet['A1':'Z1'])

 new_inp
= int(input('Type first column'))
 new_inp2
= int(input('Type second column'))
 first_col
= sheet.columns[new_inp]
 second_col
= sheet.columns[new_inp2]

 
for cellObj in first_col:
     list1
= [cellObj.value]
 
for cellObj in second_col:
     list2
= [cellObj.value]
 
for i,val in enumerate(list1,list2):
     
print(i,val)


Getting this: TypeError: 'list' object cannot be interpreted as an index

Charlie Clark

unread,
Dec 20, 2015, 6:39:09 AM12/20/15
to openpyx...@googlegroups.com
Am .12.2015, 11:52 Uhr, schrieb Marco Goldin <marko...@gmail.com>:

> Hi, i'm really new to openpyxl but i'm trying to get all values from 2
> columns and printing them side by side.

Hiya,

> tuple(sheet['A1':'Z1'])
>
> new_inp = int(input('Type first column'))
> new_inp2 = int(input('Type second column'))
> first_col = sheet.columns[new_inp]
> second_col = sheet.columns[new_inp2]
>
> for cellObj in first_col:
> list1 = [cellObj.value]
> for cellObj in second_col:
> list2 = [cellObj.value]
> for i,val in enumerate(list1,list2):
> print(i,val)
>
>
> Getting this: TypeError: 'list' object cannot be interpreted as an index

You're using enumerate incorrectly: the signature is sequence, start and
you've passed in two lists. You probably want to use zip:

for c1, c2 in zip(first_col, second_col):
print(c1.value, c2.value)

You can wrap this enumerate if you like.

Charlie
--
Charlie Clark
Managing Director
Clark Consulting & Research
German Office
Kronenstr. 27a
Düsseldorf
D- 40217
Tel: +49-211-600-3657
Mobile: +49-178-782-6226

Marco Goldin

unread,
Dec 20, 2015, 10:44:03 AM12/20/15
to openpyx...@googlegroups.com

Thanks! You're right, i didn't think of using zip function. Now it works, thank you very much.

--
You received this message because you are subscribed to a topic in the Google Groups "openpyxl-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/openpyxl-users/02uolA4UBkg/unsubscribe.
To unsubscribe from this group and all its topics, send an email to openpyxl-user...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages