Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python-插入错误_Python_Insert_Artificial Intelligence_Bots - Fatal编程技术网

python-插入错误

python-插入错误,python,insert,artificial-intelligence,bots,Python,Insert,Artificial Intelligence,Bots,我正在创建一个学习机器人,创建一个数据库文件,但插入错误,可以帮助我吗 守则: #kernel now ready for use while True: if mode == "voice": response = listen() else: response = raw_input("Say: ") if response == "aprender": learn = raw_input("Learn: ") f = open("da

我正在创建一个学习机器人,创建一个数据库文件,但插入错误,可以帮助我吗

守则:

#kernel now ready for use
while True:
if mode == "voice":
    response = listen()
else:
    response = raw_input("Say: ")
    if response == "aprender":
        learn = raw_input("Learn: ")
        f = open("database.aiml", "r")
        contents = f.readlines()
        f.close()

        #ERROR HERE>> contents.insert("1", "<category>\n<pattern>*</pattern>\n<template>\n", learn, "</template>\n</category>\n")

        f = open("database.aiml", "w")
        contents = "".join(contents)
        f.write(contents)
        f.close()
#内核现在可以使用了
尽管如此:
如果模式==“语音”:
response=listen()
其他:
响应=原始输入(“说:”)
如果响应==“aprender”:
学习=原始输入(“学习:”)
f=打开(“database.aiml”、“r”)
contents=f.readlines()
f、 关闭()
#此处出错>>内容。请插入(“1”,“\n*\n\n”,了解“\n\n”)
f=打开(“database.aiml”、“w”)
contents=”“.join(内容)
f、 写(内容)
f、 关闭()

你到底想在这里做什么?内容是一个列表对象,所以insert应该有两个参数:一个是索引,另一个是要插入的内容(在本例中是字符串)

我会首先尝试将其更改为以下内容,然后看看影响是什么:

contents.insert(1, "<category>\n<pattern>*</pattern>\n<template>\n" + learn \
+ "</template>\n</category>\n")
contents.插入(1),“\n*\n\n”+学习\
+“\n\n”)

您遇到的错误是什么?当我尝试使用learning:contents时,Python关闭。插入(“1”,“\n*\n\n”,learn,“\n\n”)请(重新)阅读“”,然后您的问题提供一个,包括错误消息和整个stacktrace(假设有一个)。是的,这很有效,但我正在尝试连接字符串和变量,我可以这样做吗?字符串和变量的连接是什么意思?如果您的意思是连接,那么您可以使用+号来实现(我已经更新了我的答案以反映这一点)