我正在使用python3.3并且在尝试挑选一个简单的字典时遇到了一个神秘的错误.
这是代码:
import os
import pickle
from pickle import *
os.chdir('c:/Python26/progfiles/')
def storvars(vdict):
f = open('varstor.txt','w')
pickle.dump(vdict,f,)
f.close()
return
mydict = {'name':'john','gender':'male','age':'45'}
storvars(mydict)
Run Code Online (Sandbox Code Playgroud)
我得到:
Traceback (most recent call last):
File "C:/Python26/test18.py", line 31, in <module>
storvars(mydict)
File "C:/Python26/test18.py", line 14, in storvars
pickle.dump(vdict,f,)
TypeError: must be str, not bytes
Run Code Online (Sandbox Code Playgroud) 在Python中,我正在编写一个自然语言处理模块,无法解决如何编写函数来执行以下操作.输入:从输入的句子作为短字符串导出的词性(POS)列表.列表中的某些项目本身就是列表,因为程序的该部分不知道从两种或更多种可能中选择哪个词性.例如,一个特定["DET", "NOUN", ["VERB", "NOUN"], "CONJ", ["ADJ", "ADV", "NOUN"], "ADV"]
的六个字的句子导致,即第一个字肯定是一个DET,第二个字肯定是NOUN,第三个字可以是一个VERB或NOUN,第四个字肯定是一个CONJ,第五个字可以是ADJ,ADV或NOUN第6个字肯定是ADV.
所以INPUT = ["DET", "NOUN", ["VERB", "NOUN"], "CONJ", ["ADJ", "ADV", "NOUN"], "ADV"]
我需要函数将每个可能的组合作为列表列表返回.所以上面的返回值应该是:
[["DET", "NOUN", "NOUN", "CONJ", "NOUN", "ADV"],
["DET", "NOUN", "NOUN", "CONJ", "ADV", "ADV"],
["DET", "NOUN", "NOUN", "CONJ", "ADJ", "ADV"],
["DET", "NOUN", "VERB", "CONJ", "NOUN", "ADV"],
["DET", "NOUN", "VERB", "CONJ", "ADV", "ADV"],
["DET", "NOUN", "VERB", "CONJ", "ADJ", "ADV"]]
Run Code Online (Sandbox Code Playgroud)
句子可以是从1到n个单词长.每个单词可能会从一个到两个部分的语音回来.