Product code looks like abcd2343, what to split by letters and numbers

后端 未结 6 930
不知归路
不知归路 2020-12-02 18:34

I have a list of product codes in a text file, on each like is the product code that looks like:

abcd2343 abw34324 abc3243-23A

6条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-02 19:22

    To partition on the first digit

    parts = re.split('(\d.*)','abcd2343')      # => ['abcd', '2343', '']
    parts = re.split('(\d.*)','abc3243-23A')   # => ['abc', '3243-23A', '']
    

    So the two parts are always parts[0] and parts[1].

    Of course, you can apply this to multiple codes:

    >>> s = "abcd2343 abw34324 abc3243-23A"
    >>> results = [re.split('(\d.*)', pcode) for pcode in s.split(' ')]
    >>> results
    [['abcd', '2343', ''], ['abw', '34324', ''], ['abc', '3243-23A', '']]
    

    If each code is in an individual line then instead of s.split( ) use s.splitlines().

提交回复
热议问题