Python TypeError:序列项12:应为str实例,未找到非类型
这是我的密码:Python TypeError:序列项12:应为str实例,未找到非类型,python,string,list,append,indexof,Python,String,List,Append,Indexof,这是我的密码: import re with open('newfiles.txt') as f: s = f.read() uniquelist = [] error = re.findall(r"[\w]+|[^\s\w]", (s)) for word in error: if word not in uniquelist: uniquelist.append(word) print ("Here are the words in their first a
import re
with open('newfiles.txt') as f:
s = f.read()
uniquelist = []
error = re.findall(r"[\w]+|[^\s\w]", (s))
for word in error:
if word not in uniquelist:
uniquelist.append(word)
print ("Here are the words in their first appearing index form: ")
my_indexes = ' '.join(str(uniquelist.index(word)+1) for word in error)
print (my_indexes)
file = open("newfiletwo.txt","w")
file.write (' '.join(str(my_indexes)))
file.close()
file = open("newfilethree.txt","w")
file.write(' '.join(uniquelist))
file.close()
word_base = None
with open('newfilethree.txt', 'rt') as f_base:
word_base = [None] + [z.strip() for z in f_base.read().split()]
sentence_seq = None
with open('newfiletwo.txt', 'rt') as f_select:
sentence_seq = [word_base[int(i)] for i in f_select.read().split()]
print(my_indexes)
print(' '.join(sentence_seq))
它获取一个文本文件并返回其中单词和标点符号的位置(索引)。如果某项操作重复,则给出遇到的第一个位置索引。所以它会打印出索引。第二,在将单独的单词保存为文件和索引列表后,我尝试使用它们重新创建文本。因此,最终输出应该是带有标点符号的原始句子。但不幸的是,当程序运行完最后一行代码时,我得到了以下错误:
Traceback (most recent call last):
File "E:\Python\Final.py", line 26, in <module>
print(' '.join(sentence_seq))
TypeError: sequence item 12: expected str instance, NoneType found
回溯(最近一次呼叫最后一次):
文件“E:\Python\Final.py”,第26行,在
打印(“”.join(句子顺序))
TypeError:序列项12:应为str实例,未找到非类型
有人知道问题出在哪里吗?你能把
打印(“.”join(句子顺序))
包装如下并发布结果吗:
try:
print(" ".join(sentence_seq))
except TypeError:
print("Broken sentence: " + repr(sentence_seq))
raise
请参见上的示例
这不是一个修复程序(但与上述一样,可能有助于故障排除): 将删除
None
s,但不会首先修复它们是None
的原因
这是一个修复程序:
my_indexes = ' '.join(str(uniquelist.index(word)) for word in error)
&
您正在将1添加到索引中,然后将
None
添加到列表的开头。。。删除这两个选项后,问题将不再重现。这不是答案,这只是为了方便查看-
Bjorn这是显示的结果:
'They' <class 'str'>
'say' <class 'str'>
'it' <class 'str'>
"'" <class 'str'>
's' <class 'str'>
'a' <class 'str'>
'dog' <class 'str'>
"'" <class 'str'>
's' <class 'str'>
'life' <class 'str'>
',' <class 'str'>
'They' <class 'str'>
None <class 'NoneType'>
'They' <class 'str'>
'They' <class 'str'>
'They' <class 'str'>
'say' <class 'str'>
嗯,
句子seq[12]
是无
。@thebjorn你是什么意思?我对python整体来说是相当陌生的word\u base[0]
是无
。因此,如果f_select.read().split()
的任何元素是0
,则将None
放入语句的该元素中。但是join()
希望所有列表元素都是字符串。错误消息告诉您,在执行print(''.join(句子顺序))
时,发生了一个类型错误,因为在“序列项12”(即句子顺序[12]
)处,当需要字符串时,出现了一个无
值。Pythons“”。join(lst)
要求lst
的所有元素都是string类型。@b好的,让我调整一下,现在处理代码“except”下的无效语法是否确实正确复制了它?这在Python2.7和Python3 for Me中都适用。您如何在注释中发布一些代码?抱歉,超出主题,请编辑您的原始问题。你就是OP。你介意把你的代码再写一遍吗?除了“except”之外的所有东西都应该有这样的缩进吗?你能以你应该被展示的方式展示它吗?不要生气,我不是想粗鲁,只是不清楚,有点混乱,就像我说的我不是python专家,我有点新你应该编辑你的原始帖子来包含这些信息。不要学我的坏榜样;)的确但是您确实注意到了第13行的None
(如果您从零开始计数,位置12)。这是一个错误。那里不应该有一个None
(因为None不是一个字符串。找出None是如何到达那里并修复它的…-)我不知道所有的东西都转换成了单独的字符串,在我看来,对于这部分问题没有别的办法。我遗漏了什么吗?问题是,当你认为程序以一种方式工作,而Python告诉你它以另一种方式工作时,你需要弄清楚你对程序的想法是如何错误的。这叫做。。。调试;-)调试基本上是划分+检查+重复。在程序中找到创建错误值的变量的位置,并在该位置检查变量(打印)。继续执行此操作,直到找到问题。语句\u-seq
从此行获取其值:语句\u-seq=[word\u-base[int(i)],用于f\u-select.read().split()]
。也许你需要打印出word\u base
。。还有f_select.read().split()
。。
word_base = [z.strip() for z in f_base.read().split()]
'They' <class 'str'>
'say' <class 'str'>
'it' <class 'str'>
"'" <class 'str'>
's' <class 'str'>
'a' <class 'str'>
'dog' <class 'str'>
"'" <class 'str'>
's' <class 'str'>
'life' <class 'str'>
',' <class 'str'>
'They' <class 'str'>
None <class 'NoneType'>
'They' <class 'str'>
'They' <class 'str'>
'They' <class 'str'>
'say' <class 'str'>
They say it's a dog's life, but for Estrella, born without her front legs, she's adapted to more of a kangaroo way of living. The Peruvian mutt hasn't let her disability hold her back, gaining celebrity status in the small town of Tinga Maria.