Saving a Python dictionary in external file?

杀马特。学长 韩版系。学妹 提交于 2019-12-10 11:17:10

问题


I'm working on a code that is essentially a super basic AI system (basically a simple Python version of Cleverbot).

As part of the code, I've got a starting dictionary with a couple keys that have lists as the values. As the file runs, the dictionary is modified - keys are created and items are added to the associated lists.

So what I want to do is have the dictionary saved as an external file in the same file folder, so that the program doesn't have to "re-learn" the data each time I start the file. So it will load it at the start of running the file, and at the end it will save the new dictionary in the external file. How can I do this?

Do I have to do this using JSON, and if so, how do I do it? Can I do it using the built-in json module, or do I need to download JSON? I tried to look up how to use it but couldn't really find any good explanations.

I have my main file saved in C:/Users/Alex/Dropbox/Coding/AI-Chat/AI-Chat.py

The phraselist is saved in C:/Users/Alex/Dropbox/Coding/AI-Chat/phraselist.py

I'm running Python 2.7 through Canopy.

When I run the code, this is the output:

In [1]: %run "C:\Users\Alex\Dropbox\Coding\AI-Chat.py"
  File "C:\Users\Alex\Dropbox\Coding\phraselist.py", line 2
    S'How are you?'
    ^
SyntaxError: invalid syntax

EDIT: I got it now. I had to specify the sys.path to import phrase frome phraselist.py

Here's the full code I have:

############################################
################ HELPER CODE ###############
############################################
import sys
import random
import json
sys.path = ['C:\\Users\\Alex\\Dropbox\\Coding\\AI-Chat'] #needed to specify path
from phraselist import phrase



def chooseResponse(prev,resp):
    '''Chooses a response from previously learned responses in phrase[resp]    
    resp: str
    returns str'''
    if len(phrase[resp])==0: #if no known responses, randomly choose new phrase
        key=random.choice(phrase.keys())
        keyPhrase=phrase[key]
        while len(keyPhrase)==0:
            key=random.choice(phrase.keys())
            keyPhrase=phrase[key]
        else:
            return random.choice(keyPhrase)
    else:
        return random.choice(phrase[resp])

def learnPhrase(prev, resp):
    '''prev is previous computer phrase, resp is human response
    learns that resp is good response to prev
    learns that resp is a possible computer phrase, with no known responses

    returns None
    '''
    #learn resp is good response to prev
    if prev not in phrase.keys():
        phrase[prev]=[]
        phrase[prev].append(resp)
    else:
        phrase[prev].append(resp) #repeat entries to weight good responses

    #learn resp is computer phrase
    if resp not in phrase.keys():
        phrase[resp]=[]

############################################
############## END HELPER CODE #############
############################################

def chat():
    '''runs a chat with Alan'''
    keys = phrase.keys()
    vals = phrase.values()

    print("My name is Alan.")
    print("I am an Artifical Intelligence Machine.")
    print("As realistic as my responses may seem, you are talking to a machine.")
    print("I learn from my conversations, so I get better every time.")
    print("Please forgive any incorrect punctuation, spelling, and grammar.")
    print("If you want to quit, please type 'QUIT' as your response.")
    resp = raw_input("Hello! ")

    prev = "Hello!"

    while resp != "QUIT":
        learnPhrase(prev,resp)
        prev = chooseResponse(prev,resp)
        resp = raw_input(prev+' ')
    else:
        with open('phraselist.py','w') as f:
            f.write('phrase = '+json.dumps(phrase))
        print("Goodbye!")

chat()

And phraselist.py looks like:

phrase = {
    'Hello!':['Hi!'],
    'How are you?':['Not too bad.'],
    'What is your name?':['Alex'],
}

回答1:


You can use pickle module for that. This module have two methods,

  1. Pickling(dump): Convert Python objects into string representation.
  2. Unpickling(load): Retrieving original objects from stored string representstion.

https://docs.python.org/3.3/library/pickle.html code:

>>> import pickle
>>> l = [1,2,3,4]
>>> with open("test.txt", "wb") as fp:   #Pickling
...   pickle.dump(l, fp)
... 
>>> with open("test.txt", "rb") as fp:   # Unpickling
...   b = pickle.load(fp)
... 
>>> b
[1, 2, 3, 4]

Following is sample code for our problem:

  1. Define phrase file name and use same file name during create/update phrase data and also during get phrase data.
  2. Use exception handling during get phrase data i.e. check if file is present or not on disk by os.path.isfile(file_path) method.
  3. As use dump and load pickle methods to set and get phrase.

code:

import os
import pickle
file_path = "/home/vivek/Desktop/stackoverflow/phrase.json"

def setPhrase():
    phrase = {
        'Hello!':['Hi!'],
        'How are you?':['Not too bad.'],
        'What is your name?':['Alex'],
    }
    with open(file_path, "wb") as fp:
        pickle.dump(phrase, fp)

    return 

def getPhrase(): 
    if os.path.isfile(file_path):
        with open(file_path, "rb") as fp: 
            phrase = pickle.load(fp)
    else:
        phrase = {}

    return phrase

if __name__=="__main__":
    setPhrase()

    #- Get values.
    phrase = getPhrase()
    print "phrase:", phrase

output:

vivek@vivek:~/Desktop/stackoverflow$ python 22.py
phrase: {'How are you?': ['Not too bad.'], 'What is your name?': ['Alex'], 'Hello!': ['Hi!']}



回答2:


You can dump it in json (built into python so you don't need to install it)

import json 
json.dump(your_dictionary, open('file_name.json', 'wb'))

You can use pickle but the file will not be human readable. Pickling is useful when you need to store python (or custom) objects.




回答3:


Using cPickles, it can store any python structure to a file

import cPickles as p
p.dump([Your Data], [Your File])

No matter it's a list, set, dictionary or what else.




回答4:


If you have it stored in a file in the same directory, you could do:

phraselist.py

phrase = {'Hello!':['Hi!'],'How are you?':['Not too bad.'],
      'What is your name?':['Alex']
     }

and in your other file do:

from phraselist import phrase

then you can reference phrase as you wish. If you actually want to modify the module, you could keep track of the dict throughout your program and just before you exit, save it back to the python file with its new contents. There might be a more elegant way to do this, but...it should work.

right before you exit:

with open('phraselist.py', 'w') as f:
   f.write('phrase = '+ json.dumps(phrase))

interpreter output:

Python 2.7.3 (default, Sep 26 2013, 20:08:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from phraselist import phrase
>>> phrase
{'How are you?': ['Not too bad.'], 'What is your name?': ['Alex'], 'Hello!': ['Hi!']}
>>> phrase['Goodbye'] = ['See you later']
>>> phrase
{'How are you?': ['Not too bad.'], 'What is your name?': ['Alex'], 'Goodbye': ['See you later'], 'Hello!': ['Hi!']}
>>> import json
>>> with open('phraselist.py', 'w') as f:
...   f.write('phrase = ' + json.dumps(phrase))
...
>>>
>>> exit()
XXX@ubuntu:~$ python
Python 2.7.3 (default, Sep 26 2013, 20:08:41)
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from phraselist import phrase
>>> phrase
{'How are you?': ['Not too bad.'], 'What is your name?': ['Alex'], 'Goodbye': ['See you later'], 'Hello!': ['Hi!']}
>>>

YOUR CODE:

phraselist.py:

phrase = {'Hello!':['Hi!'],'How are you?':['Not too bad.'],'What is your name?':['Alex']}

output from running

XXXX@ubuntu:~$ python AI-Chat.py
My name is Alan.
I am an Artifical Intelligence Machine.
As realistic as my responses may seem, you are talking to a machine.
I learn from my conversations, so I get better every time.
Please forgive any incorrect punctuation, spelling, and grammar.
If you want to quit, please type 'QUIT' as your response.
Hello! hey
Alex what's up?
Not too bad. cool
cool what do you do?
Not too bad. ...okay
what's up? not much, you?
what do you do? I'm a software engineer, what about you?
hey ...hey
not much, you? i'm going to stop now
Alex Goodbye!
i'm going to stop now sigh...
hey QUIT
Goodbye!

XXX@ubuntu:$vi phraselist.py
phrase = {"...okay": [], "not much, you?": ["i'm going to stop now"], "Alex": ["what's up?", "Goodbye!"], "i'm going to stop now": ["sigh..."], "What is your name?": ["Alex"], "Not too bad.": ["cool",     "...okay"], "hey": ["...hey"], "...hey": [], "How are you?": ["Not too bad."], "sigh...": [], "what do you do?": ["I'm a software engineer, what about you?"], "what's up?": ["not much, you?"], "Goodb    ye!": [], "Hello!": ["Hi!", "hey"], "I'm a software engineer, what about you?": [], "cool": ["what do you do?"]}

the one modification I made in AI-Chat.py:

while resp != "QUIT":
    learnPhrase(prev,resp)
    prev = chooseResponse(prev,resp)
    resp = raw_input(prev+' ')
else:
    with open('phraselist.py','w') as f:
        f.write('phrase = '+json.dumps(phrase))
    print("Goodbye!")


来源:https://stackoverflow.com/questions/28572420/saving-a-python-dictionary-in-external-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!