help with list element parsing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jumperbl
    New Member
    • Mar 2008
    • 17

    #1

    help with list element parsing

    I have an element in a list which equals='42 61 73 73 20 43 6f 6d 70 65 6e 73 61 74 69 6f \n\r6e '

    Sometimes this element has \n\r in it and sometime it doesn't. how do i make it get rid of it when it is there so it would look like this:

    '42 61 73 73 20 43 6f 6d 70 65 6e 73 61 74 69 6f r6e '

    thanks for the help
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Use the string function strip(chars) to remove the characters you don't want, eg [CODE=python]elem.strip('\r\ n')[/code]

    Comment

    • bvdet
      Recognized Expert Specialist
      • Oct 2006
      • 2851

      #3
      Laharl would be correct if the unwanted characters were leading or trailing. In your case, you can use string method replace(). [code=Python]>>> elem = '42 61 73 73 20 43 6f 6d 70 65 6e 73 61 74 69 6f \n\r6e '
      >>> elem.replace('\ n\r', '')
      '42 61 73 73 20 43 6f 6d 70 65 6e 73 61 74 69 6f 6e '
      >>> [/code]

      Comment

      Working...