在外部文件中保存Python字典?

时间:2021-11-29 20:27:24

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

我正在研究一个基本上是超级基本AI系统的代码(基本上是一个简单的Cleverbot Python版本)。

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.

我是否必须使用JSON执行此操作,如果是,我该如何操作?我可以使用内置的json模块来完成,还是需要下载JSON?我试图查找如何使用它,但无法找到任何好的解释。

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

我将我的主文件保存在C:/Users/Alex/Dropbox/Coding/AI-Chat/AI-Chat.py

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

该表达者保存在C:/Users/Alex/Dropbox/Coding/AI-Chat/phraselist.py中

I'm running Python 2.7 through Canopy.

我正在通过Canopy运行Python 2.7。

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

编辑:我现在明白了。我必须指定sys.path来导入短语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:

而phraselist.py看起来像:

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

4 个解决方案

#1


4  

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

你可以使用pickle模块。这个模块有两种方法,

  1. Pickling(dump): Convert Python objects into string representation.
  2. Pickling(转储):将Python对象转换为字符串表示形式。
  3. Unpickling(load): Retrieving original objects from stored string representstion.
  4. 取消(加载):从存储的字符串表示中检索原始对象。

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

https://docs.python.org/3.3/library/pickle.html代码:

>>> 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. 在创建/更新短语数据期间以及获取短语数据期间定义短语文件名并使用相同的文件名。
  3. 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.
  4. 在获取短语数据期间使用异常处理,即通过os.path.isfile(file_path)方法检查磁盘上是否存在文件。
  5. As use dump and load pickle methods to set and get phrase.
  6. 使用dump和load pickle方法来设置和获取短语。

code:

码:

import os
import pickle
file_path = "/home/vivek/Desktop/*/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/*$ python 22.py
phrase: {'How are you?': ['Not too bad.'], 'What is your name?': ['Alex'], 'Hello!': ['Hi!']}

#2


3  

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

你可以把它转储到json中(内置到python中,所以你不需要安装它)

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.

你可以使用泡菜,但文件不会是人类可读的。当您需要存储python(或自定义)对象时,Pickling非常有用。

#3


0  

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

使用cPickles,它可以将任何python结构存储到文件中

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

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

无论是列表,集合,字典还是其他什么。

#4


0  

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

如果将它存储在同一目录中的文件中,则可以执行以下操作:

phraselist.py

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.

然后你可以根据自己的意愿引用短语。如果你真的想要修改模块,你可以在整个程序中跟踪dict,在退出之前,将它保存回python文件及其新内容。可能有更优雅的方式来做到这一点,但......它应该有效。

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:

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:

我在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!")

#1


4  

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

你可以使用pickle模块。这个模块有两种方法,

  1. Pickling(dump): Convert Python objects into string representation.
  2. Pickling(转储):将Python对象转换为字符串表示形式。
  3. Unpickling(load): Retrieving original objects from stored string representstion.
  4. 取消(加载):从存储的字符串表示中检索原始对象。

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

https://docs.python.org/3.3/library/pickle.html代码:

>>> 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. 在创建/更新短语数据期间以及获取短语数据期间定义短语文件名并使用相同的文件名。
  3. 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.
  4. 在获取短语数据期间使用异常处理,即通过os.path.isfile(file_path)方法检查磁盘上是否存在文件。
  5. As use dump and load pickle methods to set and get phrase.
  6. 使用dump和load pickle方法来设置和获取短语。

code:

码:

import os
import pickle
file_path = "/home/vivek/Desktop/*/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/*$ python 22.py
phrase: {'How are you?': ['Not too bad.'], 'What is your name?': ['Alex'], 'Hello!': ['Hi!']}

#2


3  

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

你可以把它转储到json中(内置到python中,所以你不需要安装它)

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.

你可以使用泡菜,但文件不会是人类可读的。当您需要存储python(或自定义)对象时,Pickling非常有用。

#3


0  

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

使用cPickles,它可以将任何python结构存储到文件中

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

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

无论是列表,集合,字典还是其他什么。

#4


0  

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

如果将它存储在同一目录中的文件中,则可以执行以下操作:

phraselist.py

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.

然后你可以根据自己的意愿引用短语。如果你真的想要修改模块,你可以在整个程序中跟踪dict,在退出之前,将它保存回python文件及其新内容。可能有更优雅的方式来做到这一点,但......它应该有效。

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:

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:

我在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!")