【问题标题】:how do I breakdown a string in multiple componentshow do I breakdown a string in multiple components
【发布时间】:2022-12-27 22:36:17
【问题描述】:

I have a string 'ABCAPITAL23JAN140CE'. This is the symbol for an option traded on stock exchange. ABCAPITAL part of the string is the company name. 23 is year 2023. JAN is for month. 140 is the strike price and CE is the type of the option.

All these components can vary for different options.

I need a function such thatpieces_of_string = splitstring('ABCAPITAL23JAN140CE')

wherepieces_of_string = ['ABCAPITAL', 23, 'JAN', 140, 'CE']is returned

how do I do that?

【问题讨论】:

    标签: python string


    【解决方案1】:

    You might use re.findall with [A-Z]+|d+

    See the matches here on regex101

    import re
    print(re.findall(r"[A-Z]+|d+", "ABCAPITAL23JAN140CE"))
    

    Output

    ['ABCAPITAL', '23', 'JAN', '140', 'CE']
    

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2022-01-04
      • 2022-08-17
      • 2022-12-01
      • 2022-12-01
      • 2022-12-01
      • 2022-12-01
      • 2022-12-27
      • 2022-12-01
      相关资源
      最近更新 更多