using the string functions (ex. find()) on a multi-symbol string

Discussion in 'Python' started by korean_dave, Jun 17, 2008.

  1. korean_dave

    korean_dave Guest

    How can i use the find() function on a string that is composed of tons
    of symbols that cause errors...

    THis is my string:

    find("<html><head><meta name="qrichtext" content="1" /><style
    type="text/css">p, li { white-space: pre-wrap; }</style></head><body
    style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:
    400; font-style:normal; text-decoration:none;"><p style=" margin-top:
    0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-
    indent:0; text-indent:0px; font-size:8pt;"><span style=" font-size:
    10pt; color:green;">Connected!</span></p></body></html>","margin")

    The tough part about this is that the string is dynamically produced.
    So I can't manually go into the string and eliminate the quote-marks
    or to "literal-character" them.
    korean_dave, Jun 17, 2008
    #1
    1. Advertising

  2. korean_dave

    John Machin Guest

    On Jun 18, 7:12 am, korean_dave <> wrote:
    > How can i use the find() function on a string that is composed of tons
    > of symbols that cause errors...
    >
    > THis is my string:
    >
    > find("<html><head><meta name="qrichtext" content="1" /><style
    > type="text/css">p, li { white-space: pre-wrap; }</style></head><body
    > style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:
    > 400; font-style:normal; text-decoration:none;"><p style=" margin-top:
    > 0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-
    > indent:0; text-indent:0px; font-size:8pt;"><span style=" font-size:
    > 10pt; color:green;">Connected!</span></p></body></html>","margin")
    >
    > The tough part about this is that the string is dynamically produced.
    > So I can't manually go into the string and eliminate the quote-marks
    > or to "literal-character" them.


    What are you trying to find? What error(s) do you get?
    John Machin, Jun 17, 2008
    #2
    1. Advertising

  3. korean_dave

    John Machin Guest

    On Jun 18, 7:12 am, korean_dave <> wrote:
    > How can i use the find() function on a string that is composed of tons
    > of symbols that cause errors...
    >
    > THis is my string:
    >
    > find("<html><head><meta name="qrichtext" content="1" /><style
    > type="text/css">p, li { white-space: pre-wrap; }</style></head><body
    > style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:
    > 400; font-style:normal; text-decoration:none;"><p style=" margin-top:
    > 0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-
    > indent:0; text-indent:0px; font-size:8pt;"><span style=" font-size:
    > 10pt; color:green;">Connected!</span></p></body></html>","margin")
    >
    > The tough part about this is that the string is dynamically produced.
    > So I can't manually go into the string and eliminate the quote-marks
    > or to "literal-character" them.


    If as you say the string is dynamically created, your script should
    have a variable name for it e.g. dynstr so all you have to do is:
    dynstr.find("margin")
    Note: you should be using str methods (see http://docs.python.org/lib/string-methods.html);
    almost all functionality in the string module is now deprecated and
    redirected (slowly) e.g.
    def find(s, t):
    return s.find(t)

    If you really want/need to put such a monster string containing both '
    and " as a literal in your script, you can use triple quotes (""" or
    ''').

    I.e.
    find("""<html><head><meta name="qrichtext" ... </span></p></body></
    html>""", "margin")

    See http://docs.python.org/tut/node5.html#SECTION005120000000000000000

    HTH,
    John
    John Machin, Jun 17, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xiangliang Meng
    Replies:
    1
    Views:
    1,581
    Victor Bazarov
    Jun 21, 2004
  2. baumann@pan
    Replies:
    1
    Views:
    732
    Richard Bos
    Apr 15, 2005
  3. powah
    Replies:
    5
    Views:
    2,085
    Arne Vajhøj
    Mar 22, 2009
  4. Song Ma
    Replies:
    2
    Views:
    221
    Charles Oliver Nutter
    Jul 20, 2008
  5. Replies:
    6
    Views:
    1,737
Loading...

Share This Page