Storing parts of string in Array in python

前端 未结 1 1584
情书的邮戳
情书的邮戳 2021-01-28 09:52

My string contains large paragraph like this:

Line= \"

      Name       =    AB    | 1-2 |      Name
      ID         =    CD    | 3-4 |      int
      Stu              


        
1条回答
  •  离开以前
    2021-01-28 10:27

    EDIT: Used your string.

    Note: There's probably a cleaner way to do this.

    You can use regex:

    >>> import re
    >>> Line = """
    
      Name       =    AB    | 1-2 |      Name
      ID         =    CD    | 3-4 |      int
      Stu        =    EF    | 5-6 |      Name
      Email      =    GH    | 7-8 |      string
      ID         =    IJ    | 9-10 |     int
      Tea        =    KL    | 1--12 |    Name
      Email      =    MN    | 13-14 |    Name
      ID         =    OP    | 1-2 |      int """
    >>> Line = '\n'.join(i.lstrip() for i in Line.strip().splitlines())
    >>> newlist = [i.strip('\n') for i in re.split(r'ID.*',Line)]
    >>> print newlist[0]
    Name       =    AB    | 1-2 |      Name
    >>> print newlist[1]
    Stu        =    EF    | 5-6 |      Name
    Email      =    GH    | 7-8 |      string
    >>> print newlist[2]
    Tea        =    KL    | 1--12 |    Name
    Email      =    MN    | 13-14 |    Name
    

    0 讨论(0)
提交回复
热议问题