代码之家  ›  专栏  ›  技术社区  ›  Arvinth Kumar

从python中的字符串提取数字时出错

  •  0
  • Arvinth Kumar  · 技术社区  · 6 年前

    我正在循环处理列

        'US-Eco.Metric8_MomChg'.'US-Eco.Metric9_MomChg','US-Eco.Metric10_MomChg'
    

    我需要从字符串中提取整数并将其保存在数组中。我正在使用以下代码,但得到了一个错误。请帮帮我。

      for col in self.columns[]:
       country, market = col.split('-')
       num = []
       num.append([int (s) for market in str.split() if market.isdigit ()])
    

    错误信息:

       TypeError: descriptor 'split' of 'str' object needs an argument
    

    预期输出:

        [8,9,10]
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Sunitha    6 年前

    num = [int(s) for s in market if s.isdigit()]

    re.findall int

    >>> import re
    >>> s = "'US-Eco.Metric8_MomChg'.'US-Eco.Metric9_MomChg','US-Eco.Metric10_MomChg'"
    >>> list(map(int, re.findall(r'\d+', s)))
    [8, 9, 10]