我有这个大脚本(如果必须的话,我将发布整个内容,但它非常大),当我运行它时它开始没问题但是它立刻给了我'TypeError:无法连接'str'和'NoneType'对象的最后一点代码:
with open("self.txt", "a+") as f:
f = open("self.txt", "a+")
text = f.readlines()
text_model = markovify.Text(text)
for i in range(1):
tool = grammar_check.LanguageTool('en-GB')
lin = (text_model.make_sentence(tries=800))
word = ('' + lin)
matches = tool.check (word)
correct = grammar_check.correct (word, matches)
print ">",
print correct
print ' '
f = open("self.txt", "a+")
f.write(correct + "\n")
我到处搜寻但无处可去。它似乎与:word = ('' + lin)
有关。但无论我做什么,我都无法解决它。我做错了什么?
答案 0 :(得分:0)
我不知道我是怎么做到的,但有点摆弄和谷歌我想出了一个解决方案,更正后的代码就在这里(如果你对此感兴趣):
with open("self.txt", "a+") as f:
f = open("self.txt", "a+")
text = f.readlines()
text_model = markovify.Text(text)
for i in range(1):
tool = grammar_check.LanguageTool ('en-GB')
lin = (text_model.make_sentence(tries=200))
matches = tool.check (lin)
correct = grammar_check.correct (lin, matches)
lowcor = (correct.lower())
print ">",
print str (lowcor)
print ' '
f = open("self.txt", "a+")
f.write(lowcor + "\n")
感谢所有回复,他们让我思考,以及我是如何解决的!
答案 1 :(得分:-1)
您无法连接字符串和NoneType对象。在您的代码中,您的变量lin
似乎没有被赋予您认为的值。您可以尝试以这样开头的if
块:
if type(lin) == str:
some code
else:
raise Exception('lin is not the correct datatype')
在打印前验证lin
是否为正确的数据类型。