Using xlrd module to turn xls into Python array












2














I have a column of dates formatted as strings in Excel. I need to use them in a Python script as an array, so I used this script to convert them into an array.



import xlrd
workbook = xlrd.open_workbook('/Users/reallymemorable/Documents/output.xlsx')
worksheet = workbook.sheet_by_name('Sheet1')
num_rows = worksheet.nrows
curr_row = 0

#creates an array to store all the rows
row_array =

while curr_row < num_rows:
row = worksheet.row(curr_row)
row_array += row
curr_row += 1

print(row_array[0])


But the output is



text:'09/30/2018-09/26/2018'



instead of



09/30/2018-09/26/2018



Is there a way to address this in xlrd? Or do I need to use another module?



Here is an example input column:



09/30/2018-09/26/2018
09/25/2018-09/21/2018
09/20/2018-09/16/2018
09/15/2018-09/11/2018
09/10/2018-09/06/2018
09/05/2018-09/01/2018
08/31/2018-08/27/2018


EDIT:



I have tried to use .value to get rid of the text in this way:



while curr_row < num_rows:
row_array.append(worksheet.row(curr_row).value)
curr_row += 1


But I get this error:



  File "xlrd.test.py", line 13, in <module>
row_array.append(worksheet.row(curr_row).value)
AttributeError: 'list' object has no attribute 'value'


I also tried the list comprehension method mentioned below, and got a similar error.










share|improve this question





























    2














    I have a column of dates formatted as strings in Excel. I need to use them in a Python script as an array, so I used this script to convert them into an array.



    import xlrd
    workbook = xlrd.open_workbook('/Users/reallymemorable/Documents/output.xlsx')
    worksheet = workbook.sheet_by_name('Sheet1')
    num_rows = worksheet.nrows
    curr_row = 0

    #creates an array to store all the rows
    row_array =

    while curr_row < num_rows:
    row = worksheet.row(curr_row)
    row_array += row
    curr_row += 1

    print(row_array[0])


    But the output is



    text:'09/30/2018-09/26/2018'



    instead of



    09/30/2018-09/26/2018



    Is there a way to address this in xlrd? Or do I need to use another module?



    Here is an example input column:



    09/30/2018-09/26/2018
    09/25/2018-09/21/2018
    09/20/2018-09/16/2018
    09/15/2018-09/11/2018
    09/10/2018-09/06/2018
    09/05/2018-09/01/2018
    08/31/2018-08/27/2018


    EDIT:



    I have tried to use .value to get rid of the text in this way:



    while curr_row < num_rows:
    row_array.append(worksheet.row(curr_row).value)
    curr_row += 1


    But I get this error:



      File "xlrd.test.py", line 13, in <module>
    row_array.append(worksheet.row(curr_row).value)
    AttributeError: 'list' object has no attribute 'value'


    I also tried the list comprehension method mentioned below, and got a similar error.










    share|improve this question



























      2












      2








      2







      I have a column of dates formatted as strings in Excel. I need to use them in a Python script as an array, so I used this script to convert them into an array.



      import xlrd
      workbook = xlrd.open_workbook('/Users/reallymemorable/Documents/output.xlsx')
      worksheet = workbook.sheet_by_name('Sheet1')
      num_rows = worksheet.nrows
      curr_row = 0

      #creates an array to store all the rows
      row_array =

      while curr_row < num_rows:
      row = worksheet.row(curr_row)
      row_array += row
      curr_row += 1

      print(row_array[0])


      But the output is



      text:'09/30/2018-09/26/2018'



      instead of



      09/30/2018-09/26/2018



      Is there a way to address this in xlrd? Or do I need to use another module?



      Here is an example input column:



      09/30/2018-09/26/2018
      09/25/2018-09/21/2018
      09/20/2018-09/16/2018
      09/15/2018-09/11/2018
      09/10/2018-09/06/2018
      09/05/2018-09/01/2018
      08/31/2018-08/27/2018


      EDIT:



      I have tried to use .value to get rid of the text in this way:



      while curr_row < num_rows:
      row_array.append(worksheet.row(curr_row).value)
      curr_row += 1


      But I get this error:



        File "xlrd.test.py", line 13, in <module>
      row_array.append(worksheet.row(curr_row).value)
      AttributeError: 'list' object has no attribute 'value'


      I also tried the list comprehension method mentioned below, and got a similar error.










      share|improve this question















      I have a column of dates formatted as strings in Excel. I need to use them in a Python script as an array, so I used this script to convert them into an array.



      import xlrd
      workbook = xlrd.open_workbook('/Users/reallymemorable/Documents/output.xlsx')
      worksheet = workbook.sheet_by_name('Sheet1')
      num_rows = worksheet.nrows
      curr_row = 0

      #creates an array to store all the rows
      row_array =

      while curr_row < num_rows:
      row = worksheet.row(curr_row)
      row_array += row
      curr_row += 1

      print(row_array[0])


      But the output is



      text:'09/30/2018-09/26/2018'



      instead of



      09/30/2018-09/26/2018



      Is there a way to address this in xlrd? Or do I need to use another module?



      Here is an example input column:



      09/30/2018-09/26/2018
      09/25/2018-09/21/2018
      09/20/2018-09/16/2018
      09/15/2018-09/11/2018
      09/10/2018-09/06/2018
      09/05/2018-09/01/2018
      08/31/2018-08/27/2018


      EDIT:



      I have tried to use .value to get rid of the text in this way:



      while curr_row < num_rows:
      row_array.append(worksheet.row(curr_row).value)
      curr_row += 1


      But I get this error:



        File "xlrd.test.py", line 13, in <module>
      row_array.append(worksheet.row(curr_row).value)
      AttributeError: 'list' object has no attribute 'value'


      I also tried the list comprehension method mentioned below, and got a similar error.







      python xlrd






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 13:05

























      asked Nov 13 '18 at 5:18









      reallymemorable

      151119




      151119
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Instead of:



          print(row_array[0])
          # text:'09/30/2018-09/26/2018


          Specify the value:



          print(row_array[0].value)
          # 09/30/2018-09/26/2018


          The issue here is that row_array[0] is an xlrd cell, and to access the content of this cell, you need to add value.



          To apply this change to all the cells, you can use a list comprehension:



          row_array = [row.value for row in row_array]


          Or you can just ensure that the array appends the actual value to begin with:



          row_array.append([row.value for row in worksheet.row(curr_row)])





          share|improve this answer























          • I tried your methods, but I am getting errors. I edited my original post above.
            – reallymemorable
            Nov 13 '18 at 13:05










          • @reallymemorable try row_array.append([row.value for row in worksheet.row(curr_row)])
            – RoadRunner
            Nov 13 '18 at 13:24











          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',
          autoActivateHeartbeat: false,
          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%2f53274270%2fusing-xlrd-module-to-turn-xls-into-python-array%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









          0














          Instead of:



          print(row_array[0])
          # text:'09/30/2018-09/26/2018


          Specify the value:



          print(row_array[0].value)
          # 09/30/2018-09/26/2018


          The issue here is that row_array[0] is an xlrd cell, and to access the content of this cell, you need to add value.



          To apply this change to all the cells, you can use a list comprehension:



          row_array = [row.value for row in row_array]


          Or you can just ensure that the array appends the actual value to begin with:



          row_array.append([row.value for row in worksheet.row(curr_row)])





          share|improve this answer























          • I tried your methods, but I am getting errors. I edited my original post above.
            – reallymemorable
            Nov 13 '18 at 13:05










          • @reallymemorable try row_array.append([row.value for row in worksheet.row(curr_row)])
            – RoadRunner
            Nov 13 '18 at 13:24
















          0














          Instead of:



          print(row_array[0])
          # text:'09/30/2018-09/26/2018


          Specify the value:



          print(row_array[0].value)
          # 09/30/2018-09/26/2018


          The issue here is that row_array[0] is an xlrd cell, and to access the content of this cell, you need to add value.



          To apply this change to all the cells, you can use a list comprehension:



          row_array = [row.value for row in row_array]


          Or you can just ensure that the array appends the actual value to begin with:



          row_array.append([row.value for row in worksheet.row(curr_row)])





          share|improve this answer























          • I tried your methods, but I am getting errors. I edited my original post above.
            – reallymemorable
            Nov 13 '18 at 13:05










          • @reallymemorable try row_array.append([row.value for row in worksheet.row(curr_row)])
            – RoadRunner
            Nov 13 '18 at 13:24














          0












          0








          0






          Instead of:



          print(row_array[0])
          # text:'09/30/2018-09/26/2018


          Specify the value:



          print(row_array[0].value)
          # 09/30/2018-09/26/2018


          The issue here is that row_array[0] is an xlrd cell, and to access the content of this cell, you need to add value.



          To apply this change to all the cells, you can use a list comprehension:



          row_array = [row.value for row in row_array]


          Or you can just ensure that the array appends the actual value to begin with:



          row_array.append([row.value for row in worksheet.row(curr_row)])





          share|improve this answer














          Instead of:



          print(row_array[0])
          # text:'09/30/2018-09/26/2018


          Specify the value:



          print(row_array[0].value)
          # 09/30/2018-09/26/2018


          The issue here is that row_array[0] is an xlrd cell, and to access the content of this cell, you need to add value.



          To apply this change to all the cells, you can use a list comprehension:



          row_array = [row.value for row in row_array]


          Or you can just ensure that the array appends the actual value to begin with:



          row_array.append([row.value for row in worksheet.row(curr_row)])






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 13:24

























          answered Nov 13 '18 at 5:29









          RoadRunner

          10.7k31340




          10.7k31340












          • I tried your methods, but I am getting errors. I edited my original post above.
            – reallymemorable
            Nov 13 '18 at 13:05










          • @reallymemorable try row_array.append([row.value for row in worksheet.row(curr_row)])
            – RoadRunner
            Nov 13 '18 at 13:24


















          • I tried your methods, but I am getting errors. I edited my original post above.
            – reallymemorable
            Nov 13 '18 at 13:05










          • @reallymemorable try row_array.append([row.value for row in worksheet.row(curr_row)])
            – RoadRunner
            Nov 13 '18 at 13:24
















          I tried your methods, but I am getting errors. I edited my original post above.
          – reallymemorable
          Nov 13 '18 at 13:05




          I tried your methods, but I am getting errors. I edited my original post above.
          – reallymemorable
          Nov 13 '18 at 13:05












          @reallymemorable try row_array.append([row.value for row in worksheet.row(curr_row)])
          – RoadRunner
          Nov 13 '18 at 13:24




          @reallymemorable try row_array.append([row.value for row in worksheet.row(curr_row)])
          – RoadRunner
          Nov 13 '18 at 13:24


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274270%2fusing-xlrd-module-to-turn-xls-into-python-array%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

          Bressuire

          Vorschmack

          Quarantine