• Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
MagicMirror Forum
  • Recent
  • Tags
  • Unsolved
  • Solved
  • MagicMirror² Repository
  • Documentation
  • 3rd-Party-Modules
  • Donate
  • Discord
  • Register
  • Login
A New Chapter for MagicMirror: The Community Takes the Lead
Read the statement by Michael Teeuw here.

save some information from a xml or json to a txt file

Scheduled Pinned Locked Moved Requests
25 Posts 5 Posters 16.4k Views 5 Watching
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    gismo2006
    last edited by Nov 13, 2016, 10:28 AM

    @LukeCodewalker Hi, YEAH! THANK YOU! Python will be OK for me. I have tested a python script, that I found at the www. Lat days. But it didn’t work. Late today if I have some time I will post some more information OK?

    Greets gismo

    L 1 Reply Last reply Nov 13, 2016, 11:49 AM Reply Quote 0
    • L Offline
      LukeCodewalker Module Developer @gismo2006
      last edited by Nov 13, 2016, 11:49 AM

      @gismo2006 sure =)

      1 Reply Last reply Reply Quote 0
      • G Offline
        gismo2006
        last edited by Nov 13, 2016, 4:30 PM

        @LukeCodewalker

        Here some more information:

        I use this link to use the api (It the same website which is use for the default Weather modul)

        http://api.openweathermap.org/data/2.5/weather?id=2934486&lang=de&units=metric&mode=xml&appid=

        (I only cut out my api key at he end)

        This link create a xml file (You can change into json, simply erase xml in the link and put json there)

        This xml looks like this:

        DE

        And, I only like that your python script read out the line till and if it possible the line <temperature, too. And the save this in a txt file.

        That will be great.

        And if I can a have little bit more of your time it will be possible the txt file can use the following "Today it is and .

        It would be perfekt. But that I can took later by learning myself. If your python script can read out the xml file and save in a txt file it will be great.

        Greets gismo

        L 1 Reply Last reply Nov 13, 2016, 5:49 PM Reply Quote 0
        • L Offline
          LukeCodewalker Module Developer @gismo2006
          last edited by Nov 13, 2016, 5:49 PM

          @gismo2006 here it is. just place your app id and the path of the file, where the text should go.

          import json
          import subprocess
          
          APP_ID = ''
          FILE_PATH = 'test.txt'
          
          url = 'http://api.openweathermap.org/data/2.5/weather?id=2934486&lang=de&units=metric&mode=json&appid=' + APP_ID
          curl_req = 'curl -L ' + '"' + url + '"'
          p = subprocess.Popen(curl_req, stdout=subprocess.PIPE, shell=True)
          (output, err) = p.communicate()  
          p_status = p.wait()
          
          try:
              decoded = json.loads(output)
              weather = decoded['weather'][0]['description']
              temp = decoded['main']['temp']
              f = open(FILE_PATH,'w')
              f.write('Today is ' + weather + ' and ' + str(temp) + ' degrees.')
              f.close()
          except (ValueError, KeyError, TypeError):
              pass
          
          1 Reply Last reply Reply Quote 0
          • G Offline
            gismo2006
            last edited by Nov 14, 2016, 7:33 AM

            @LukeCodewalker

            Hi, thank you for the code. But there was a little misstake I think. I have copied your code and save. put in my api and the right folder.

            then the first try. All works fine the code generate a test.txt file. But then I oen the txt file it was empty.
            Ok I think maybe I put in something wrong. But everything was ok.

            At a second try I chnage the folder name, because I think maybe the was some folder Rights or something else. But the same. The txt file was empty.

            At my next try I cut of the f.write line and there was only written f.write(‘Today is’)
            And I see everything work like it should. In the txt file was written Today is. Now I put some more in this line again. Everything works fine till I put the word weather. If I put this the txt file will be empty.

            I like to try something mor later today. But I want give you a feedback. And I like to say thank you.

            greets gismo

            1 Reply Last reply Reply Quote 0
            • G Offline
              gismo2006
              last edited by Nov 14, 2016, 7:40 AM

              Sorry I forgett there was no error. Also Python displays no error if I try it a the Python IDLE.

              L 1 Reply Last reply Nov 14, 2016, 10:29 AM Reply Quote 0
              • L Offline
                LukeCodewalker Module Developer @gismo2006
                last edited by Nov 14, 2016, 10:29 AM

                @gismo2006 I think I got the problem. The german description can contain letters like äöü etc. i will fix that later when i’m at home.

                1 Reply Last reply Reply Quote 0
                • L Offline
                  LukeCodewalker Module Developer
                  last edited by Nov 14, 2016, 2:56 PM

                  Now it should work

                  #!/usr/bin/python
                  # -*- coding: utf-8 -*-
                  import codecs
                  import json
                  import subprocess
                  
                  APP_ID = ''
                  FILE_PATH = 'test.txt'
                  
                  url = 'http://api.openweathermap.org/data/2.5/weather?id=2934486&lang=de&units=metric&mode=json&appid=' + APP_ID
                  curl_req = 'curl -L ' + '"' + url + '"'
                  p = subprocess.Popen(curl_req, stdout=subprocess.PIPE, shell=True)
                  (output, err) = p.communicate()  
                  p_status = p.wait()
                  
                  try:
                      decoded = json.loads(output)
                      weather = decoded['weather'][0]['description']
                      temp = decoded['main']['temp']
                      f = codecs.open(FILE_PATH,'w', 'utf-8')
                      f.write('Today is ' + weather + ' and ' + str(temp) + ' degrees.')
                      f.close()
                  except (ValueError, KeyError, TypeError):
                      pass
                  
                  1 Reply Last reply Reply Quote 0
                  • G Offline
                    gismo2006
                    last edited by Nov 14, 2016, 5:14 PM

                    Hi, a big thank you!!

                    Its work perfect!!!

                    Many Thanks! :-)

                    greets gismo

                    M 1 Reply Last reply Jan 7, 2017, 1:10 AM Reply Quote 0
                    • L Offline
                      LukeCodewalker Module Developer
                      last edited by Nov 14, 2016, 6:04 PM

                      Welcome :)

                      S 1 Reply Last reply Jan 1, 2017, 7:34 PM Reply Quote 0
                      • 1
                      • 2
                      • 3
                      • 1 / 3
                      1 / 3
                      • First post
                        9/25
                        Last post
                      Enjoying MagicMirror? Please consider a donation!
                      MagicMirror created by Michael Teeuw.
                      Forum managed by Sam, technical setup by Karsten.
                      This forum is using NodeBB as its core | Contributors
                      Contact | Privacy Policy