Request XML file from URL and save as CSV using python












0















I am new in python coding and I would like to get XML file from a server, parse it and save to csv file.



2 parts are ok, I am able to get the file and parse it, but there is an issue with saving as a csv.



The code:



import requests
import numpy as np

hu = requests.get('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml', stream=True)
from xml.etree import ElementTree as ET
tree = ET.parse(hu.raw)
root = tree.getroot()
namespaces = {'ex': 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'}
for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
np.savetxt('data.csv', (cube.attrib['currency'], cube.attrib['rate']), delimiter=',')


Error I get is: mismatch between array dtype and format specifier.
It probably means I get data and try to save it as array, and there appears a mismatch.
But i am not sure how to fix the problem and to not have a mismatch.



Thank you










share|improve this question



























    0















    I am new in python coding and I would like to get XML file from a server, parse it and save to csv file.



    2 parts are ok, I am able to get the file and parse it, but there is an issue with saving as a csv.



    The code:



    import requests
    import numpy as np

    hu = requests.get('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml', stream=True)
    from xml.etree import ElementTree as ET
    tree = ET.parse(hu.raw)
    root = tree.getroot()
    namespaces = {'ex': 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'}
    for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
    np.savetxt('data.csv', (cube.attrib['currency'], cube.attrib['rate']), delimiter=',')


    Error I get is: mismatch between array dtype and format specifier.
    It probably means I get data and try to save it as array, and there appears a mismatch.
    But i am not sure how to fix the problem and to not have a mismatch.



    Thank you










    share|improve this question

























      0












      0








      0








      I am new in python coding and I would like to get XML file from a server, parse it and save to csv file.



      2 parts are ok, I am able to get the file and parse it, but there is an issue with saving as a csv.



      The code:



      import requests
      import numpy as np

      hu = requests.get('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml', stream=True)
      from xml.etree import ElementTree as ET
      tree = ET.parse(hu.raw)
      root = tree.getroot()
      namespaces = {'ex': 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'}
      for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
      np.savetxt('data.csv', (cube.attrib['currency'], cube.attrib['rate']), delimiter=',')


      Error I get is: mismatch between array dtype and format specifier.
      It probably means I get data and try to save it as array, and there appears a mismatch.
      But i am not sure how to fix the problem and to not have a mismatch.



      Thank you










      share|improve this question














      I am new in python coding and I would like to get XML file from a server, parse it and save to csv file.



      2 parts are ok, I am able to get the file and parse it, but there is an issue with saving as a csv.



      The code:



      import requests
      import numpy as np

      hu = requests.get('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml', stream=True)
      from xml.etree import ElementTree as ET
      tree = ET.parse(hu.raw)
      root = tree.getroot()
      namespaces = {'ex': 'http://www.ecb.int/vocabulary/2002-08-01/eurofxref'}
      for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
      np.savetxt('data.csv', (cube.attrib['currency'], cube.attrib['rate']), delimiter=',')


      Error I get is: mismatch between array dtype and format specifier.
      It probably means I get data and try to save it as array, and there appears a mismatch.
      But i am not sure how to fix the problem and to not have a mismatch.



      Thank you







      python xml csv






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 '18 at 16:42









      bennesbennes

      2127




      2127
























          1 Answer
          1






          active

          oldest

          votes


















          0














          from the docs, your second argument in np.savetext should be a tuple of equal sized arrays. What you are providing are strings:



          >>> x = y = z = np.arange(0.0,5.0,1.0)
          >>> np.savetxt('test.out', x, delimiter=',') # X is an array
          >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays
          >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation


          You'll need to gather all of the concurrency and rate values into arrays, then save as csv:



          concurrency, rate = , 
          for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
          concurrency.append(cube.attrib['concurrency'])
          rate.append(cube.attrib['rate'])

          np.savetext('file.csv', (concurrency, rate), delimeter='c')





          share|improve this answer
























          • how can I gather all of the currency and rate values into arrays? How to fill an array from first part of the code please? Or is there a way how not to use arrays in this case at all? Just to get the data and fill the file?

            – bennes
            Nov 21 '18 at 12:08











          • @bennes look at what's happening in the for loop. Run the code and you'll see what's going on

            – C.Nivs
            Nov 21 '18 at 17:22











          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%2f53397629%2frequest-xml-file-from-url-and-save-as-csv-using-python%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














          from the docs, your second argument in np.savetext should be a tuple of equal sized arrays. What you are providing are strings:



          >>> x = y = z = np.arange(0.0,5.0,1.0)
          >>> np.savetxt('test.out', x, delimiter=',') # X is an array
          >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays
          >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation


          You'll need to gather all of the concurrency and rate values into arrays, then save as csv:



          concurrency, rate = , 
          for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
          concurrency.append(cube.attrib['concurrency'])
          rate.append(cube.attrib['rate'])

          np.savetext('file.csv', (concurrency, rate), delimeter='c')





          share|improve this answer
























          • how can I gather all of the currency and rate values into arrays? How to fill an array from first part of the code please? Or is there a way how not to use arrays in this case at all? Just to get the data and fill the file?

            – bennes
            Nov 21 '18 at 12:08











          • @bennes look at what's happening in the for loop. Run the code and you'll see what's going on

            – C.Nivs
            Nov 21 '18 at 17:22
















          0














          from the docs, your second argument in np.savetext should be a tuple of equal sized arrays. What you are providing are strings:



          >>> x = y = z = np.arange(0.0,5.0,1.0)
          >>> np.savetxt('test.out', x, delimiter=',') # X is an array
          >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays
          >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation


          You'll need to gather all of the concurrency and rate values into arrays, then save as csv:



          concurrency, rate = , 
          for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
          concurrency.append(cube.attrib['concurrency'])
          rate.append(cube.attrib['rate'])

          np.savetext('file.csv', (concurrency, rate), delimeter='c')





          share|improve this answer
























          • how can I gather all of the currency and rate values into arrays? How to fill an array from first part of the code please? Or is there a way how not to use arrays in this case at all? Just to get the data and fill the file?

            – bennes
            Nov 21 '18 at 12:08











          • @bennes look at what's happening in the for loop. Run the code and you'll see what's going on

            – C.Nivs
            Nov 21 '18 at 17:22














          0












          0








          0







          from the docs, your second argument in np.savetext should be a tuple of equal sized arrays. What you are providing are strings:



          >>> x = y = z = np.arange(0.0,5.0,1.0)
          >>> np.savetxt('test.out', x, delimiter=',') # X is an array
          >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays
          >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation


          You'll need to gather all of the concurrency and rate values into arrays, then save as csv:



          concurrency, rate = , 
          for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
          concurrency.append(cube.attrib['concurrency'])
          rate.append(cube.attrib['rate'])

          np.savetext('file.csv', (concurrency, rate), delimeter='c')





          share|improve this answer













          from the docs, your second argument in np.savetext should be a tuple of equal sized arrays. What you are providing are strings:



          >>> x = y = z = np.arange(0.0,5.0,1.0)
          >>> np.savetxt('test.out', x, delimiter=',') # X is an array
          >>> np.savetxt('test.out', (x,y,z)) # x,y,z equal sized 1D arrays
          >>> np.savetxt('test.out', x, fmt='%1.4e') # use exponential notation


          You'll need to gather all of the concurrency and rate values into arrays, then save as csv:



          concurrency, rate = , 
          for cube in root.findall('.//ex:Cube[@currency]', namespaces=namespaces):
          concurrency.append(cube.attrib['concurrency'])
          rate.append(cube.attrib['rate'])

          np.savetext('file.csv', (concurrency, rate), delimeter='c')






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 20 '18 at 16:51









          C.NivsC.Nivs

          2,0661414




          2,0661414













          • how can I gather all of the currency and rate values into arrays? How to fill an array from first part of the code please? Or is there a way how not to use arrays in this case at all? Just to get the data and fill the file?

            – bennes
            Nov 21 '18 at 12:08











          • @bennes look at what's happening in the for loop. Run the code and you'll see what's going on

            – C.Nivs
            Nov 21 '18 at 17:22



















          • how can I gather all of the currency and rate values into arrays? How to fill an array from first part of the code please? Or is there a way how not to use arrays in this case at all? Just to get the data and fill the file?

            – bennes
            Nov 21 '18 at 12:08











          • @bennes look at what's happening in the for loop. Run the code and you'll see what's going on

            – C.Nivs
            Nov 21 '18 at 17:22

















          how can I gather all of the currency and rate values into arrays? How to fill an array from first part of the code please? Or is there a way how not to use arrays in this case at all? Just to get the data and fill the file?

          – bennes
          Nov 21 '18 at 12:08





          how can I gather all of the currency and rate values into arrays? How to fill an array from first part of the code please? Or is there a way how not to use arrays in this case at all? Just to get the data and fill the file?

          – bennes
          Nov 21 '18 at 12:08













          @bennes look at what's happening in the for loop. Run the code and you'll see what's going on

          – C.Nivs
          Nov 21 '18 at 17:22





          @bennes look at what's happening in the for loop. Run the code and you'll see what's going on

          – C.Nivs
          Nov 21 '18 at 17:22


















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53397629%2frequest-xml-file-from-url-and-save-as-csv-using-python%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

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]