heroService.heroes
我想从文件中打印import nltk
import pickle
input_file=open('file.txt', 'r')
input_datafile=open('newskills1.txt', 'r')
string=input_file.read()
fp=(input_datafile.read().splitlines())
def extract_skills(string):
skills=pickle.load(fp)
skill_set=[]
for skill in skills:
skill= ''+skill+''
if skill.lower() in string:
skill_set.append(skill)
return skill_set
if __name__ == '__main__':
skills= extract_skills(string)
print(skills)
但是,此处skills
无法正常工作
它显示错误:
_pickle.UnpicklingError:必须引用STRING操作码参数
答案 0 :(得分:2)
必须将包含pickle数据的文件写入并读取为二进制文件。有关示例,请参阅the documentation。
您的提取功能应如下所示:
def extract_skills(path):
with open(path, 'rb') as inputFile:
skills = pickle.load(inputFile)
当然,您需要将数据转储到以二进制形式打开的文件中:
def save_skills(path, skills):
with open(path, 'wb') as outputFile:
pickle.dump(outputFile, skills)
此外,你的主要逻辑似乎有点瑕疵。
虽然if __name__ == '__main__'
后面的代码仅在脚本作为主模块运行时执行,但main中不的代码应该只是静态的,即定义。
基本上,您的脚本不应该执行任何操作,除非以main身份运行。
这是一个更清洁的版本。
import pickle
def extract_skills(path):
...
def save_skills(path, skills):
...
if __name__ == '__main__':
inputPath = "skills_input.pickle"
outputPath = "skills_output.pickle"
skills = extract_skills(inputPath)
# Modify skills
save_skills(outputPath, skills)