簡體   English   中英

類型錯誤:無法排序的類型:int() < str()

[英]TypeError: unorderable types: int() < str()

當我在 JSON 新聞數據集上應用 5W1H 提取器(這是 Git 中的一個開源庫)時發生錯誤。

嘗試運行時,在evaluate_location 文件中發生錯誤

raw_locations.sort(key=lambda x: x[1], reverse=True)

然后控制台給出了錯誤說

TypeError: unorderable types: int() < str()

我的問題是:這是否意味着我的數據集格式有問題? 但如果是這樣,當提取器在這個語料庫上工作時,它不應該將所有新聞數據視為一個簡單的長字符串嗎? 我急切地尋找解決這個問題的方法。

這是json新聞數據之一:

{
"title": "Football: Van Dijk, Ronaldo and Messi shortlisted for FIFA award",
"body": "ROME: Liverpool centre-back Virgil van Dijk is on the shortlist to add FIFA's best player award to his UEFA Men's Player of the Year honour.The Dutch international denied Cristiano Ronaldo and Lionel Messi for the European title last week and the same trio are in the running for the FIFA accolade to be announced in Milan on September 23.    Van Dijk starred in Liverpool's triumphant Champions League campaign.England full-back Lucy Bronze won UEFA's women's award and is on FIFA's shortlist with the United States' World Cup-winning duo Megan Rapinoe and Alex Morgan.Manchester City boss Pep Guardiola is up against Liverpool's Jurgen Klopp and Mauricio Pochettino of Tottenham for best men's coach.Phil Neville, who led England's women to a World Cup semi-final, is up for the women's coach award with the USA's Jill Ellis and Sarina Wiegman who guided European champions the Netherlands to the World Cup final.    FIFA Best shortlistsMen's player:Cristiano Ronaldo (Juventus/Portugal), Lionel Messi (Barcelona/Argentina), Virgil van Dijk  player:Lucy Bronze (Lyon/England), Alex Morgan (Orlando Pride/USA), Megan Rapinoe (Reign FC/USA)Men's coach:Pep Guardiola (Manchester City), Jurgen Klopp (Liverpool), Mauricio Pochettino (Tottenham)Women's coach:Jill Ellis (USA), Phil Neville (England), Sarina Wiegman (Netherlands)Women's goalkeeper:Christiane Endler (Paris St-Germain/Chile), Hedvig Lindahl (Wolfsburg/Sweden), Sari van Veenendaal (Atletico Madrid/Netherlands)Men's goalkeeper:Alisson (Liverpool/Brazil), Ederson (Manchester City/Brazil), Marc-Andre ter Stegen (Barcelona/Germany)Puskas award (for best goal):Lionel Messi (Barcelona v Real Betis), Juan Quintero (River Plate v Racing Club), Daniel Zsori (Debrecen v Ferencvaros)",
"published_at": "2019-09-02",
} 

代碼:

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)

它沒有返回提取的時間和位置結果,而是給了我這個錯誤:

 Traceback (most recent call last):   File
 "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
     self.run()   File "/usr/local/lib/python3.5/dist-packages/Giveme5W1H/extractor/extractor.py",
 line 20, in run
     extractor.process(document)   File "/usr/local/lib/python3.5/dist-packages/Giveme5W1H/extractor/extractors/abs_extractor.py",
 line 41, in process
     self._evaluate_candidates(document)   File "/usr/local/lib/python3.5/dist-packages/Giveme5W1H/extractor/extractors/environment_extractor.py",
 line 75, in _evaluate_candidates
     locations = self._evaluate_locations(document)   File "/usr/local/lib/python3.5/dist-packages/Giveme5W1H/extractor/extractors/environment_extractor.py",
 line 224, in _evaluate_locations
     raw_locations.sort(key=lambda x: x[1], reverse=True) TypeError: unorderable types: int() < str()

row_locations 在第 219 行的同一個文件中構建:

raw_locations.append([parts, location.raw['place_id'], location.point, bb, area, 0, 0, candidate, 0])

因此, sort 函數嘗試按place_id對位置進行排序。 請檢查您的數據集是否包含place_id字符串和數字。 如果是這樣,您需要將所有條目轉換為一種類型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM