-1

I have one python file named testings.py, in this file I derive a dictionary by using web scraping of relevant data. To populate the dictionary takes a few minutes.

What I would like to do, instead of copying the whole script into my other python script named webscraper.py, and then running the whole of both scripts in effect every time I run webscraper.py, is instead import the dictionary from the testing.py script into the second script webscraper.py and then save myself those minutes everytime I run webscraper.py.

Every so often my dictionary will be out of date, in which case I would just run testings.py before starting webcraper.py. Is there a way to do what I'm asking?

Another thing to note is that, at the start of testings.py I have my_dict = {} and it's not until the end of the script that my_dict is fully populated. So when I import it I want to import the fully populated version of it as if the script had run fully.

martineau
  • 119,623
  • 25
  • 170
  • 301
pvmlad
  • 87
  • 2
  • 8
  • I'm starting to think that this isn't possible, as the testings.py script would need to run to get the my_dict dictionary populated. And my_dict wouldn't be saved anywhere on my pc to import it into webscraper.py. Unless I added some code in testings.py to save the dictionary to a csv file and then in webscraper.py I got it to read the CSV file. Maybe this would be a solution? – pvmlad Feb 05 '20 at 00:55
  • 1
    you can pickle the dictionary at whichever state you want and then read the pickle back in later. The pickle knows the object is a dictionary and has all the keys/values in the dictionary as of the time of the pickle dump. Would that achieve what you want? – Lucas Roberts Feb 05 '20 at 00:56
  • Yes, there's a way to do what you want. See [Saving an Object (Data persistence)](https://stackoverflow.com/questions/4529815/saving-an-object-data-persistence). – martineau Feb 05 '20 at 00:57

2 Answers2

1

You can either save the dictionary in a number of ways to file (check out https://docs.python.org/3/library/pickle.html or https://docs.python.org/3/library/json.html and how to write files, https://docs.python.org/3/library/io.html?highlight=open) and then read it from the other script.

Or you can just import the scraping script into the other script and call the scraping function from there, returning the result - but that's only a solution if you don't mind running the scraping there and then and you're not looking to reuse the results of an earlier scrape.

Grismar
  • 27,561
  • 4
  • 31
  • 54
0

When I run this script :

from tester import my_dict
print ('Now in second script.')
print (my_dict)

It actually runs the script called tester.py and then imports and prints my_dict.

my_dict = {}
for x in range (20) :
    for y in range (10) :
        my_dict [x] = y
print (my_dict)
input ('All done now.')
bashBedlam
  • 1,402
  • 1
  • 7
  • 11