pythonでの、ValueError: not enough values to unpack (expected 2, got 1)について
解決したいこと
pythonでpythonパーフェクトマスターを見ながら、会話botをつくっています。
発生している問題・エラー
Traceback (most recent call last):
File "ptna_from.py", line 7, in
ptna = Ptna('ptna')
File "C:\Users\momom\ptna\ptna_ptna.py", line 7, in init
self.dictionary = Dictionary()
File "C:\Users\momom\ptna\dictionary.py", line 31, in init
ptn,prs=line.split('\t')
ValueError: not enough values to unpack (expected 2, got 1)
例)
import random
import re
class Dictionary:
def __init__(self):
self.random = []
rfile=open('random.txt','r',encoding='utf-8')
r_line =rfile.readlines()
rfile.close()
self.random=[]
for line in r_line:
str =line.rstrip('\n')
if (str!=''):
self.random.append(str)
pfile = open('pattern.txt','r',encoding='utf_8')
p_lines = pfile.readlines()
pfile.close()
self.new_lines=[]
for line in p_lines:
str=line.rstrip('\n')
if(str!=''):
self.new_lines.append(str)
self.pattern=[]
for line in self.new_lines:
ptn,prs=line.split('\t')
self.pattern.append(ParseItem(ptn,prs))
class ParseItem:
SEPARATOR ='^((-?\d+)##)?(.*)$'
def __init__(self,pattern,phrases):
m = re.findall(ParseItem.SEPARATOR,pattern)
self.modify=0
if m [0][1]:
self.modify=int(m[0][1])
self.pattern=m[0][2]
self.phrases=[]
self.dic={}
for phrase in phrases.split('|'):
m = re.findall(ParseItem.SEPARATOR,phrase)
self.dic['need']=0
if m[0][1]:
self.dic['need']=int(m[0][1])
self.dic['phrase']=m[0][2]
self.phrases.append(self.dic.copy())
def match(self,str):
return re.search(self.pattern,str)
def choice(self,mood):
choices=[]
for p in self.phrases:
if (self.suitable(p['need'],mood)):
choices.append(p['phrase'])
if(len(choices)==0):
return None
return random.choice(choices)
def suitable(self,need,mood):
if (need == 0):
return True
elif (need > 0):
return(mood > need)
else:
return (mood < need)
自分で試したこと
Errorの意味を調べたり、何度も見直しました。