Passing data between separately running Python scripts

前端 未结 3 2002
眼角桃花
眼角桃花 2020-12-01 11:06

If I have a python script running (with full Tkinter GUI and everything) and I want to pass the live data it is gathering (stored internally in arrays and such) to another p

3条回答
  •  误落风尘
    2020-12-01 11:26

    You could use the pickling module to pass data between two python programs.

    import pickle 
    
    def storeData(): 
        # initializing data to be stored in db 
        employee1 = {'key' : 'Engineer', 'name' : 'Harrison', 
        'age' : 21, 'pay' : 40000} 
        employee2 = {'key' : 'LeadDeveloper', 'name' : 'Jack', 
        'age' : 50, 'pay' : 50000} 
    
        # database 
        db = {} 
        db['employee1'] = employee1 
        db['employee2'] = employee2 
    
        # Its important to use binary mode 
        dbfile = open('examplePickle', 'ab') 
    
        # source, destination 
        pickle.dump(db, dbfile)                   
        dbfile.close() 
    
    def loadData(): 
        # for reading also binary mode is important 
        dbfile = open('examplePickle', 'rb')      
        db = pickle.load(dbfile) 
        for keys in db: 
            print(keys, '=>', db[keys]) 
        dbfile.close() 
    

提交回复
热议问题