簡體   English   中英

為什么我的函數會出現“TypeError:字符串索引必須是整數”?

[英]Why am I getting "TypeError: string indices must be integers" on my function?

我有一個字符串如下:

paragraph = 'Below you’ll find KPIs (key performance indicators) and valuation metrics for 50+ public SaaS and cloud companies. This includes historical share price performance and valuation multiples, an interactive regression chart, efficiency metrics (magic number, payback period, ARR / FTE, etc.), average ACV (annual contract value), and financial metrics including ARR, OpEx margins and cash flow margins. These metrics can be filtered by year-over-year ARR growth rates (filter located under the Valuation Metrics section header). Share prices and financial data are updated as of 06-May-2022 and will continue to be updated frequently.'

我正在嘗試編寫一個函數來將日期作為字符串檢索為 '06-May-2022'

def get_date(inputString):
    # this will require a list with two elements, both integers
    boolean_list = [char.isdigit() for char in inputString]
    all_indexes = [i for i, x in enumerate(boolean_list) if x]
    all_indexes = all_indexes[2:] 
    indexes = [all_indexes[0],all_indexes[-1]]
    
    index_one = int(indexes[0])
    index_two = int(indexes[1])
    date = inputString[index_one,index_two]
    return date

    
get_date(paragraph)

但是當我運行它時,我收到錯誤消息“TypeError:字符串索引必須是整數”

當我運行這個:

type(indexes[0])

它返回“int”,所以我不明白這個錯誤。 任何幫助將不勝感激。 謝謝!

這不會直接回答您的問題,但如果您想在給定字符串中查找“日期”,您可能需要查看Regular Expressions

在 Python 中,您可能會執行類似...

import re

paragraph = """Below you’ll find KPIs (key performance indicators) and valuation metrics for 50+ public SaaS and cloud companies. This includes historical share price performance and valuation multiples, an interactive regression chart, efficiency metrics (magic number, payback period, ARR / FTE, etc.), average ACV (annual contract value), and financial metrics including ARR, OpEx margins and cash flow margins. These metrics can be filtered by year-over-year ARR growth rates (filter located under the Valuation Metrics section header). Share prices and financial data are updated as of 06-May-2022 and will continue to be updated frequently."""

exp = re.compile(r"[0-9][0-9]-.+-[0-9][0-9][0-9][0-9]")
dates = exp.search(paragraph)

if dates:
  date = dates[0]
  print(str(date))

該片段將打印'06-May-2022' 您可以在Regex101上查看有關與該字符串匹配的表達式的更多信息。

在軟件工程中,有一個概念叫做“不要重新發明輪子” :換句話說,使用正則表達式等現有技術,而不是嘗試設計復雜的函數來解析日期字符串。 更重要的是,您可能會發現一些包可以從字符串中提取日期,而無需自己使用正則表達式。

你可以在 Python 的自然語言處理中使用 spacy 模塊:

import spacy
# Load English tokenizer, tagger, parser and NER
nlp = spacy.load("en_core_web_sm")
doc = nlp(paragraph)
# Find named entities, phrases and concepts
for entity in doc.ents:
  if entity.label_== 'DATE' and str(entity)[0].isdigit():  #second condition isto select only the dates with integer values
    print(entity)

#output
'06-May-2022'

暫無
暫無

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

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