Detec nonascii in a string

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

    #1

    Detec nonascii in a string

    Hello,

    How do I detect non-ascii letters in a string?
    I want to detect the condition that a string have a letter that is not
    here: string.ascii_le tters

    Best regards,
    SB.

    --
    Bioinformatics news: http://www.bioinformatica.info
    Lriser: http://www.linspire.com/lraiser_success.php?serial=318
  • Steven D'Aprano

    #2
    Re: Detec nonascii in a string

    On Thu, 23 Feb 2006 12:32:35 -0300, Sebastian Bassi wrote:
    [color=blue]
    > Hello,
    >
    > How do I detect non-ascii letters in a string?
    > I want to detect the condition that a string have a letter that is not
    > here: string.ascii_le tters[/color]

    for c in some_string:
    if c not in string.ascii_le tters:
    raise ValueError("Non-ascii value!!!")

    Instead of raising an error, you can take whatever action you prefer:

    for c in some_string:
    if c not in string.ascii_le tters:
    print "Character '%s' is non-ascii." % repr(c)

    Or turn it into a function:

    def isascii(s):
    for c in some_string:
    if c not in string.ascii_le tters:
    return False
    return True


    --
    Steven.

    Comment

    • Gerard Flanagan

      #3
      Re: Detec nonascii in a string

      Sebastian Bassi wrote:[color=blue]
      > Hello,
      >
      > How do I detect non-ascii letters in a string?
      > I want to detect the condition that a string have a letter that is not
      > here: string.ascii_le tters
      >
      > Best regards,
      > SB.
      >
      > --
      > Bioinformatics news: http://www.bioinformatica.info
      > Lriser: http://www.linspire.com/lraiser_success.php?serial=318[/color]


      see:



      eg. ord(max(string) ) < 128 ( Scott David Daniels ).


      Gerard

      Comment

      • Diez B. Roggisch

        #4
        Re: Detec nonascii in a string

        Sebastian Bassi wrote:
        [color=blue]
        > Hello,
        >
        > How do I detect non-ascii letters in a string?
        > I want to detect the condition that a string have a letter that is not
        > here: string.ascii_le tters[/color]

        "äöü".decode ("ascii")

        should do the trick -- you get an UnicodeError when there is anything ascii
        can't encode.

        Diez

        Comment

        • Sebastian Bassi

          #5
          Re: Detec nonascii in a string

          On 2/23/06, Diez B. Roggisch <deets@nospam.w eb.de> wrote:[color=blue]
          > "äöü".decode("a scii")
          > should do the trick -- you get an UnicodeError when there is anything ascii
          > can't encode.[/color]

          Thank you. This is good enought for me.
          Best regards,
          SB.

          --
          Bioinformatics news: http://www.bioinformatica.info
          Lriser: http://www.linspire.com/lraiser_success.php?serial=318

          Comment

          Working...