Re: remove special characters from line

Discussion in 'Python' started by Chris Rennert, Jul 1, 2003.

  1. I apologize for being vague on what a "special" character is in my context.
    For me, it would be anything that isn't A..Z , a..z, or 0..9 .
    My list seems to work, or like it was suggested I could have used a string
    as well. I thank everyone for the help, and I am really enjoying learning
    Python.

    Thanks again,
    Chris Rennert
    "Chris Rennert" <> wrote in message
    news:3f01a27d$0$43847$...
    > Hello all,
    >
    > If I have a line like this
    >
    > ²blah blah blah blah blah
    >
    > I know I could do a slice like this [1:] to pull everything but the

    special
    > character, but what if I have several lines in a file.
    > I am not sure how I would detect a special character like that. I would
    > just like to pull everything from those lines (and the special character
    > always appears as the first character, but not on every line) except for

    the
    > special characters.
    > I hope I have enough detail for someone to help me.
    >
    > Thanks in advance,
    >
    > Chris
    >
    >
    Chris Rennert, Jul 1, 2003
    #1
    1. Advertising

  2. Chris Rennert

    John Machin Guest

    "Chris Rennert" <> wrote in message news:<3f01e0b0$0$43854$>...
    > I apologize for being vague on what a "special" character is in my context.
    > For me, it would be anything that isn't A..Z , a..z, or 0..9 .
    > My list seems to work, or like it was suggested I could have used a string
    > as well. I thank everyone for the help, and I am really enjoying learning
    > Python.
    >


    That's good news ... and here's a couple of more lessons:

    (1) You can use the translate method:

    # once
    import string
    id_trans = "".join([chr(x) for x in range(256)])
    good_chars = string.letters + string.digits
    bad_chars = "".join([x for x in id_trans if x not in good_chars])
    # then once for each maybe_bad_string
    good_string = maybe_bad_string.translate(id_trans, bad_chars)

    That is a bit over the top, but reading the doc to understand what is
    going down will pay dividends.

    (2) Here's a more straightforward way using the re module

    # once
    import re
    subber = re.compile(r"[^A-Za-z0-9]").sub
    # then once for each maybe_bad_string
    good_string = subber("", maybe_bad_string)
    # simpler but slower:
    good_string = re.sub(r"[^A-Za-z0-9]", "", maybe_bad_string)
    # and as a bonus extra
    show_where_junk_was_string = subber("?", maybe_bad_string)

    HTH,
    John
    John Machin, Jul 2, 2003
    #2
    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. Radovan Garabik

    Re: remove special characters from line

    Radovan Garabik, Jul 1, 2003, in forum: Python
    Replies:
    0
    Views:
    659
    Radovan Garabik
    Jul 1, 2003
  2. Egor Bolonev

    Re: remove special characters from line

    Egor Bolonev, Jul 1, 2003, in forum: Python
    Replies:
    2
    Views:
    534
    Chris Rennert
    Jul 1, 2003
  3. Bob Gailer
    Replies:
    0
    Views:
    385
    Bob Gailer
    Jul 1, 2003
  4. rvino
    Replies:
    0
    Views:
    4,635
    rvino
    Aug 14, 2007
  5. Replies:
    8
    Views:
    183
Loading...

Share This Page