Search code examples
pythonseleniumdictionaryraspberry-pi3schedule

How can i use string of data showing times (scraped from a table from a website) in python?


I need my Raspberry Pi to play sound every day at specific times. For this i'm using schedule for python:

schedule.every().day.at("20:00").do(playsound)

This works fine.

I scrape the times from a website with selenium in python. The data i get looks something like this:

29.10.2017 00:00 05:00 10:00 15:00 20:00 
30.10.2017 00:10 05:10 10:10 15:10 20:10
31.10.2017 00:00 05:50 10:10 15:15 20:20
01.11.2017 10:00 06:00 11:00 16:00 21:00
...

The question is: How can i use this data in python?

I can put the data into an array by the use of .split(), but then it becomes difficult to look it up. From the array, i can put it into a dictionary:

dict = {'Time1': arrayOfTimes[1], 'Time2': arrayOfTimes[2],...}

But do i then have to create a dictionary for each day? Or overwrite the dictionary?

If i want to play the sound at every time, every day, how can i achieve this? More precisely: I want to play the sound today at 00:00 and 05:00 and 10:00 and so on. Then i want to play the sound again tomorrow at the new times: 00:10 and 05:10 and 10:10 etc.


Solution

  • You are on to the right lines with creating an array, and a dictionary too. But you want to avoid creating dictionaries for each day separately.

    Assuming you have the data as a string data, you can use the following code:

    times = {}
    
    for x in data.split("\n"):
        split = x.split()
        times[split[0]]=split[1:]
    

    This first loops through each line of the input, and then creates an entry in the dictionary with the day as a key, and the remaining parts of the line (the times) as the value.

    times will look something like

    {'31.10.2017': ['00:00', '05:50', '10:10', '15:15', '20:20'],
    '30.10.2017': ['00:10', '05:10', '10:10', '15:10', '20:10'], 
    '29.10.2017': ['00:00', '05:00', '10:00', '15:00', '20:00'], 
    '01.11.2017': ['10:00', '06:00', '11:00', '16:00', '21:00']}
    

    Then, when you get to the right day, you can simply look at (e.g.) times['29.10.2017'] - which you can then loop through to make the appropriate calls in schedule.