【问题标题】:TypeError: sequence item 12: expected str instance, NoneType foundTypeError:序列项 12:预期的 str 实例,找到 NoneType
【发布时间】:2017-06-05 20:54:14
【问题描述】:

这是我的代码:

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

有谁知道问题出在哪里?

【问题讨论】:

  • 好吧,sentence_seq[12]None...
  • @thebjorn 你是什么意思?我对 python 很陌生
  • word_base[0]None。因此,如果f_select.read().split() 的任何元素是0,您将把None 放入sentence_seq 的那个元素中。但是join() 期望所有列表元素都是字符串。
  • 错误消息告诉您,在执行print(' '.join(sentence_seq)) 时发生类型错误,因为在“序列项 12”(即sentence_seq[12])处,需要字符串时有一个None 值反而。 Pythons ' '.join(lst) 要求 lst 的所有元素都是字符串类型。
  • @thebjorn 好的,让我稍微调整一下,现在正在处理代码

标签: python string list append indexof


【解决方案1】:

你能把 print(" ".join(sentence_seq)) 包装成下面这样并发布结果吗:

try: 
    print(" ".join(sentence_seq)) 
except TypeError: 
    print("Broken sentence: " + repr(sentence_seq))
    raise

查看repl.it上的示例


不是修复方法(但可能对故障排除有用,如上所示):

print(" ".join(filter(None, sentence_seq)))

将删除Nones,但不会解决为什么它们首先是None


这是一个修复

my_indexes = ' '.join(str(uniquelist.index(word)) for word in error)

&

word_base = [z.strip() for z in f_base.read().split()]

您将 1 添加到索引,然后将 None 添加到列表的开头...通过删除这两个问题不再重现。

【讨论】:

  • 'except'下的语法无效
  • 您确定复制正确吗?这适用于我的 Python2.7 和 Python3
  • 如何在 cmets 中发布一些代码?抱歉,题外话了
  • 编辑您的原始问题。你是 OP。
  • 您介意再次编写“答案”的代码部分吗?除了“除外”之外,其他所有内容都应该有这样的缩进吗?你能以你应该被展示的方式展示它吗?不要被冒犯,我不是要粗鲁,只是不清楚而且有点混乱,正如我所说的我不是python专家,我有点新
【解决方案2】:

这不是答案,这只是为了方便观看 - 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'>

它输出上面显示的内容,但程序的目的是打印这个:

   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.

【讨论】:

  • 您应该编辑您的原始帖子以包含此信息。不要效仿我的坏榜样;)
  • 确实如此。但是您确实注意到了第 13 行的None &lt;class 'NoneType'&gt;(如果您从零开始计数,则为第 12 位)。那是一个错误。那里不应该有None(因为 None 不是字符串。弄清楚 None 是如何到达那里并修复它的...... :-)
  • 我不知道所有内容都已转换为单独的字符串,在我看来,这部分问题没有别的可做。我错过了什么吗?
  • 问题是,当你认为程序以一种方式工作而 Python 告诉你它以不同的方式工作时,你需要弄清楚你对程序的看法是错误的。这被称为...调试;-) 调试基本上是除法+检查+重复。在程序中找到创建错误值变量的位置,并检查该点的变量(打印它们)。继续这样做,直到找到问题为止。
  • sentence_seq 从这一行获取它的值:sentence_seq = [word_base[int(i)] for i in f_select.read().split()]。也许您需要打印出word_base? .. 还有f_select.read().split()..
猜你喜欢
  • 1970-01-01
  • 2020-04-28
  • 1970-01-01
  • 2020-09-29
  • 2021-01-08
  • 2021-05-06
  • 1970-01-01
  • 2019-05-29
  • 2022-10-18
相关资源
最近更新 更多