How can I convert a column of Excel into a specific dictionary format?
up vote
-2
down vote
favorite
I have one column in my excel file, i want to convert all the rows of the column into a specific dictionary format.
that is, the keys and values between double quotation and values between brackets and parentheses and before that, the word is set, as seen in the picture:
I use this code but This does not give me the format I want:
import openpyxl
# Access active worksheet of excel file
book = openpyxl.load_workbook('workbook.xlsx')
sheet = book.active
# Access first column
column = sheet['A']
# Use dictionary comprehension on the cells in the column
d = {
'item{}'.format(num): cell.value
for (num, cell) in enumerate(column, 1)
}
python dictionary openpyxl
add a comment |
up vote
-2
down vote
favorite
I have one column in my excel file, i want to convert all the rows of the column into a specific dictionary format.
that is, the keys and values between double quotation and values between brackets and parentheses and before that, the word is set, as seen in the picture:
I use this code but This does not give me the format I want:
import openpyxl
# Access active worksheet of excel file
book = openpyxl.load_workbook('workbook.xlsx')
sheet = book.active
# Access first column
column = sheet['A']
# Use dictionary comprehension on the cells in the column
d = {
'item{}'.format(num): cell.value
for (num, cell) in enumerate(column, 1)
}
python dictionary openpyxl
add a comment |
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have one column in my excel file, i want to convert all the rows of the column into a specific dictionary format.
that is, the keys and values between double quotation and values between brackets and parentheses and before that, the word is set, as seen in the picture:
I use this code but This does not give me the format I want:
import openpyxl
# Access active worksheet of excel file
book = openpyxl.load_workbook('workbook.xlsx')
sheet = book.active
# Access first column
column = sheet['A']
# Use dictionary comprehension on the cells in the column
d = {
'item{}'.format(num): cell.value
for (num, cell) in enumerate(column, 1)
}
python dictionary openpyxl
I have one column in my excel file, i want to convert all the rows of the column into a specific dictionary format.
that is, the keys and values between double quotation and values between brackets and parentheses and before that, the word is set, as seen in the picture:
I use this code but This does not give me the format I want:
import openpyxl
# Access active worksheet of excel file
book = openpyxl.load_workbook('workbook.xlsx')
sheet = book.active
# Access first column
column = sheet['A']
# Use dictionary comprehension on the cells in the column
d = {
'item{}'.format(num): cell.value
for (num, cell) in enumerate(column, 1)
}
python dictionary openpyxl
python dictionary openpyxl
edited Nov 12 at 14:34
Anony-Mousse
56.6k796158
56.6k796158
asked Nov 11 at 5:20
mina
14
14
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
Your image shows the values in the dictionary as single value sets, rather than integers. You can put the cell.value
into a set:
d = {
'item{}'.format(num): {cell.value} for (num, cell) in enumerate(column, 1)
}
To check:
>>> from pprint import pprint
>>> pprint.pprint(d)
{'item1': {5},
'item2': {2},
'item3': {0},
'item4': {1},
'item5': {6},
'item6': {6},
'item7': {1}}
>>> pprint([type(d[key]) for key in d.keys()])
[<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>]
Thank you very very very much Orix.
– mina
Nov 11 at 17:02
Glad my answer helped you. :) If you don't mind please accept my answer and mark this question as solved as well
– Orix Au Yeung
Nov 12 at 2:48
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Your image shows the values in the dictionary as single value sets, rather than integers. You can put the cell.value
into a set:
d = {
'item{}'.format(num): {cell.value} for (num, cell) in enumerate(column, 1)
}
To check:
>>> from pprint import pprint
>>> pprint.pprint(d)
{'item1': {5},
'item2': {2},
'item3': {0},
'item4': {1},
'item5': {6},
'item6': {6},
'item7': {1}}
>>> pprint([type(d[key]) for key in d.keys()])
[<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>]
Thank you very very very much Orix.
– mina
Nov 11 at 17:02
Glad my answer helped you. :) If you don't mind please accept my answer and mark this question as solved as well
– Orix Au Yeung
Nov 12 at 2:48
add a comment |
up vote
0
down vote
accepted
Your image shows the values in the dictionary as single value sets, rather than integers. You can put the cell.value
into a set:
d = {
'item{}'.format(num): {cell.value} for (num, cell) in enumerate(column, 1)
}
To check:
>>> from pprint import pprint
>>> pprint.pprint(d)
{'item1': {5},
'item2': {2},
'item3': {0},
'item4': {1},
'item5': {6},
'item6': {6},
'item7': {1}}
>>> pprint([type(d[key]) for key in d.keys()])
[<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>]
Thank you very very very much Orix.
– mina
Nov 11 at 17:02
Glad my answer helped you. :) If you don't mind please accept my answer and mark this question as solved as well
– Orix Au Yeung
Nov 12 at 2:48
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Your image shows the values in the dictionary as single value sets, rather than integers. You can put the cell.value
into a set:
d = {
'item{}'.format(num): {cell.value} for (num, cell) in enumerate(column, 1)
}
To check:
>>> from pprint import pprint
>>> pprint.pprint(d)
{'item1': {5},
'item2': {2},
'item3': {0},
'item4': {1},
'item5': {6},
'item6': {6},
'item7': {1}}
>>> pprint([type(d[key]) for key in d.keys()])
[<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>]
Your image shows the values in the dictionary as single value sets, rather than integers. You can put the cell.value
into a set:
d = {
'item{}'.format(num): {cell.value} for (num, cell) in enumerate(column, 1)
}
To check:
>>> from pprint import pprint
>>> pprint.pprint(d)
{'item1': {5},
'item2': {2},
'item3': {0},
'item4': {1},
'item5': {6},
'item6': {6},
'item7': {1}}
>>> pprint([type(d[key]) for key in d.keys()])
[<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>,
<class 'set'>]
answered Nov 11 at 6:47
Orix Au Yeung
514
514
Thank you very very very much Orix.
– mina
Nov 11 at 17:02
Glad my answer helped you. :) If you don't mind please accept my answer and mark this question as solved as well
– Orix Au Yeung
Nov 12 at 2:48
add a comment |
Thank you very very very much Orix.
– mina
Nov 11 at 17:02
Glad my answer helped you. :) If you don't mind please accept my answer and mark this question as solved as well
– Orix Au Yeung
Nov 12 at 2:48
Thank you very very very much Orix.
– mina
Nov 11 at 17:02
Thank you very very very much Orix.
– mina
Nov 11 at 17:02
Glad my answer helped you. :) If you don't mind please accept my answer and mark this question as solved as well
– Orix Au Yeung
Nov 12 at 2:48
Glad my answer helped you. :) If you don't mind please accept my answer and mark this question as solved as well
– Orix Au Yeung
Nov 12 at 2:48
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53246060%2fhow-can-i-convert-a-column-of-excel-into-a-specific-dictionary-format%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown