【问题标题】:Saving a Python dictionary in external file?在外部文件中保存 Python 字典?
【发布时间】:2015-02-17 22:32:25
【问题描述】:

我正在编写一个本质上是超级基础人工智能系统的代码(基本上是一个简单的 Python 版本的 Cleverbot)。

作为代码的一部分,我有一个起始字典,其中包含几个以列表作为值的键。在文件运行时,字典会被修改 - 创建键并将项目添加到关联列表中。

所以我想做的是将字典作为外部文件保存在同一个文件夹中,这样程序就不必在每次启动文件时“重新学习”数据。所以它会在开始运行文件时加载它,最后它将新字典保存在外部文件中。我该怎么做?

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

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

短语列表保存在 C:/Users/Alex/Dropbox/Coding/AI-Chat/phraselist.py 中

我正在通过 Canopy 运行 Python 2.7。

当我运行代码时,这是输出:

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

编辑:我现在明白了。我必须指定 sys.path 才能从短语列表.py 中导入短语

这是我的完整代码:

############################################
################ 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()

phraselist.py 看起来像:

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

【问题讨论】:

  • 为什么不写入另一个 Python 文件,以便您可以简单地作为字典导入?

标签: python json dictionary artificial-intelligence


【解决方案1】:

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

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

您可以使用 pickle,但该文件将不适合人类阅读。当您需要存储 Python(或自定义)对象时,Pickling 很有用。

【讨论】:

  • 实际上,嗯,它必须稍微修改一下。对于您的答案,您必须每次都转储它并加载它。对于我来说,您必须加载模块,然后将 'phrase = ' +json.dumps(phrase) 输出到文件
  • 这本质上就像酸洗,但你实际上可以理解输出
  • pickle 使用较少的存储空间; cPicklepickle 快得多
【解决方案2】:

您可以为此使用pickle 模块。 这个模块有两个方法,

  1. Pickling(dump):将 Python 对象转换为字符串表示形式。
  2. Unpickling(load):从存储的字符串表示中检索原始对象。

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]

以下是我们问题的示例代码:

  1. 在创建/更新短语数据以及获取短语数据期间定义短语文件名并使用相同的文件名。
  2. 在获取短语数据期间使用异常处理,即通过os.path.isfile(file_path) 方法检查磁盘上是否存在文件。
  3. 使用dumpload pickle 方法来设置和获取短语。

代码:

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

输出:

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

【讨论】:

    【解决方案3】:

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

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

    无论是列表、集合、字典还是其他。

    【讨论】:

      【解决方案4】:

      如果您将其存储在同一目录中的文件中,您可以这样做:

      phraselist.py

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

      在你的其他文件中做:

      from phraselist import phrase
      

      然后您可以根据需要引用短语。如果你真的想修改模块,你可以在整个程序中跟踪 dict,在你退出之前,将它的新内容保存回 python 文件。可能有一种更优雅的方法可以做到这一点,但是......它应该可以工作。

      就在你退出之前:

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

      解释器输出:

      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!']}
      >>>
      

      您的代码:

      phraselist.py:

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

      运行输出

      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?"]}
      

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

      【讨论】:

      • 这似乎对我不起作用。我应该注意到我正在通过 Canopy 运行 Python 2.7(因为我正在上一门编程课,而这就是他们使用的 - 我只编码了几个星期)。有什么建议吗?
      • 您看到了什么错误输出?我将使用解释器的输出来编辑我的帖子
      • 好的,这就是我现在得到的。当我运行代码时,它显示 %run "C:\Users\Alex\Dropbox\Coding\AI-Chat.py" File "C:\Users\Alex\Dropbox\Coding\phraselist.py", line 2 S'你好吗?' ^ SyntaxError: invalid syntax 我仔细检查了我的两个文件,但我没有看到它有“你好吗?”的任何部分为什么 Python 认为有错误?我编辑了我的原始帖子以包含我的完整代码。
      • 去掉你在phraselist.py中的任何空格...它应该只有一行。这可能会解决您的问题
      • 您的代码似乎对我有用...(排序,大声笑)我将使用运行您粘贴的代码获得的输出更新我的答案(稍作修改)
      猜你喜欢
      • 2016-07-09
      • 2018-12-20
      • 2016-03-29
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 2013-07-13
      • 2011-06-21
      • 1970-01-01
      相关资源
      最近更新 更多