Picking apart strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • tmallen

    Picking apart strings

    Is there a way to pick apart this text without resorting to regular
    expressions?

    p {
    color: black;
    }

    p -element
    color -property
    black -value
  • jay graves

    #2
    Re: Picking apart strings

    On Jun 3, 1:44 pm, tmallen <thomasmal...@g mail.comwrote:
    Is there a way to pick apart this text without resorting to regular
    expressions?
    >
    p {
    color: black;
    >
    }
    >
    p -element
    color -property
    black -value


    Comment

    • Russ P.

      #3
      Re: Picking apart strings

      On Jun 3, 11:44 am, tmallen <thomasmal...@g mail.comwrote:
      Is there a way to pick apart this text without resorting to regular
      expressions?
      >
      p {
      color: black;
      >
      }
      >
      p -element
      color -property
      black -value
      Sure.

      data = txt.strip("}"). split("{")

      element = data[0].strip()

      items = data[1].split(";")

      for item in items:

      data = item.split(":")
      property = data[0].strip() # avoid this keyword
      value = data[1].strip()

      I didn't test this. Also, the module suggested in the other reply
      might make more sense, depending on how much of this sort of thing you
      need to do.

      Comment

      Working...