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)
}









share|improve this question




























    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)
    }









    share|improve this question


























      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)
      }









      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 14:34









      Anony-Mousse

      56.6k796158




      56.6k796158










      asked Nov 11 at 5:20









      mina

      14




      14
























          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'>]





          share|improve this answer





















          • 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











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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

























          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'>]





          share|improve this answer





















          • 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















          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'>]





          share|improve this answer





















          • 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













          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'>]





          share|improve this answer












          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'>]






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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


















          • 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


















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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





















































          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







          Popular posts from this blog

          Full-time equivalent

          Bicuculline

          さくらももこ