Python TypeError:无序类型:int()<;str()
在JSON新闻数据集上应用5W1H提取器(Git中的开源库)时发生错误 尝试运行时,错误发生在位置文件处Python TypeError:无序类型:int()<;str(),python,python-3.x,Python,Python 3.x,在JSON新闻数据集上应用5W1H提取器(Git中的开源库)时发生错误 尝试运行时,错误发生在位置文件处 raw_locations.sort(key=lambda x: x[1], reverse=True) 然后控制台给出了一个错误,说 TypeError: unorderable types: int() < str() 代码: 它没有返回提取的时间和位置结果,而是给了我以下错误: 回溯(最近一次调用上次):文件 “/usr/lib/python3.5/threading.py”
raw_locations.sort(key=lambda x: x[1], reverse=True)
然后控制台给出了一个错误,说
TypeError: unorderable types: int() < str()
代码:
它没有返回提取的时间和位置结果,而是给了我以下错误:
回溯(最近一次调用上次):文件
“/usr/lib/python3.5/threading.py”,第914行,在内部引导中
self.run()文件“/usr/local/lib/python3.5/dist-packages/Giveme5W1H/extractor/extractor.py”,
第20行,运行中
extractor.process(document)文件“/usr/local/lib/python3.5/dist packages/Giveme5W1H/extractor/extractors/abs_extractor.py”,
第41行,正在处理中
self._evaluate_候选者(文档)文件“/usr/local/lib/python3.5/dist packages/Giveme5W1H/extractor/extractors/environment_extractor.py”,
第75行,内部评估候选人
locations=self.\u evaluation\u locations(文档)文件“/usr/local/lib/python3.5/dist packages/Giveme5W1H/extractor/extractors/environment\u extractor.py”,
第224行,在“评估”位置
raw_locations.sort(key=lambda x:x[1],reverse=True)类型错误:无序类型:int()
行位置构建在第219行的同一文件中:
raw_locations.append([parts, location.raw['place_id'], location.point, bb, area, 0, 0, candidate, 0])
因此,sort函数尝试按位置的
place\u id
对位置进行排序。请检查您的数据集是否包含place\u id
的字符串和数字。如果是这样,您需要将所有条目转换为一种类型。尝试更改doc1=Document.from\u text(text,date\u publish)
。它会抛出相同的错误吗?@MjZac恐怕不会,该方法需要所有四个输入元素。谢谢,那么place_id需要什么内容?在对原始数据应用提取方法之前,我需要转换它们吗?我不知道应该是什么类型。看看你的数据,试着找出它。我猜应该是数字,因为字段名为place\u id。。。
json_file = open("./Labeled.json","r",encoding="utf-8")
data = json.load(json_file)
if __name__ == '__main__':
# logger setup
log = logging.getLogger('GiveMe5W')
log.setLevel(logging.DEBUG)
sh = logging.StreamHandler()
sh.setLevel(logging.DEBUG)
log.addHandler(sh)
# giveme5w setup - with defaults
extractor = MasterExtractor()
Document()
for i in range(0,1000):
body = data[i]["body"]
#print(body)
#for line in body:
#print(line[0:line.find('\n')])
#head = re.sub("[^A-Z\d]", "", "")
head = re.search("^[^\n]*", body).group(0)
head = str(head)
title = data[i]["title"]
title = str(title)
body = data[i]["body"]
body = str(body)
published_at = data[i]["published_at"]
published_at = str(published_at)
doc1 = Document(title,head,body,published_at)
doc = extractor.parse(doc1)
raw_locations.append([parts, location.raw['place_id'], location.point, bb, area, 0, 0, candidate, 0])