Character Sequence Generation

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

    #1

    Character Sequence Generation

    What's the best way to generate a sequence of characters in Python? I'm
    looking for something like this Perl code: 'a' .. 'z' .
  • mensanator@aol.com

    #2
    Re: Character Sequence Generation


    Jeff Schwab wrote:[color=blue]
    > What's the best way to generate a sequence of characters in Python? I'm
    > looking for something like this Perl code: 'a' .. 'z' .[/color]
    [color=blue][color=green][color=darkred]
    >>> import string[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    >>> print string.ascii_lo wercase[/color][/color][/color]
    abcdefghijklmno pqrstuvwxyz


    Others:

    ascii_letters = 'abcdefghijklmn opqrstuvwxyzABC DEFGHIJKLMNOPQR STUVWXYZ'
    ascii_lowercase = 'abcdefghijklmn opqrstuvwxyz'
    ascii_uppercase = 'ABCDEFGHIJKLMN OPQRSTUVWXYZ'
    digits = '0123456789'
    hexdigits = '0123456789abcd efABCDEF'
    letters =
    'ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuv...\xcd\xce ...
    lowercase =
    'abcdefghijklmn opqrstuvwxyz\x8 3\x9a\x9c\x9e\x aa\xb5\xba\xd.. .
    octdigits = '01234567'
    printable =
    '0123456789abcd efghijklmnopqrs tuvwxyzABCDEFGH IJKLMNOPQRSTU.. .
    punctuation = '!"#$%&\'()*+ ,-./:;<=>?@[\\]^_`{|}~'
    uppercase =
    'ABCDEFGHIJKLMN OPQRSTUVWXYZ\x8 a\x8c\x8e\x9f\x c0\xc1\xc2\xc.. .
    whitespace = '\t\n\x0b\x0c\r '

    Comment

    • Jeff Schwab

      #3
      Re: Character Sequence Generation

      mensanator@aol. com wrote:[color=blue]
      > Jeff Schwab wrote:
      >[color=green]
      >>What's the best way to generate a sequence of characters in Python? I'm
      >>looking for something like this Perl code: 'a' .. 'z' .[/color]
      >
      >[color=green][color=darkred]
      >>>>import string[/color][/color]
      >
      >[color=green][color=darkred]
      >>>>print string.ascii_lo wercase[/color][/color]
      >
      > abcdefghijklmno pqrstuvwxyz[/color]

      Thanks. Is there a good way to generate the characters dynamically?

      Comment

      • Devan L

        #4
        Re: Character Sequence Generation


        Jeff Schwab wrote:[color=blue]
        > mensanator@aol. com wrote:[color=green]
        > > Jeff Schwab wrote:
        > >[color=darkred]
        > >>What's the best way to generate a sequence of characters in Python? I'm
        > >>looking for something like this Perl code: 'a' .. 'z' .[/color]
        > >
        > >[color=darkred]
        > >>>>import string[/color]
        > >
        > >[color=darkred]
        > >>>>print string.ascii_lo wercase[/color]
        > >
        > > abcdefghijklmno pqrstuvwxyz[/color]
        >
        > Thanks. Is there a good way to generate the characters dynamically?[/color]

        ''.join([chr(i) for i in range(97,123)])

        Comment

        • Pedro Werneck

          #5
          Re: Character Sequence Generation

          On Thu, 22 Sep 2005 23:26:58 -0400
          Jeff Schwab <jeffrey.schwab @rcn.com> wrote:
          [color=blue]
          > What's the best way to generate a sequence of characters in Python?
          > I'm looking for something like this Perl code: 'a' .. 'z' .[/color]

          If you want arbitrary sequences, you may use something like:

          [color=blue][color=green][color=darkred]
          >>> [chr(x) for x in xrange(ord('a') , ord('z') + 1)][/color][/color][/color]
          ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
          'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
          [color=blue][color=green][color=darkred]
          >>> [chr(x) for x in xrange(ord('d') , ord('p') + 1)][/color][/color][/color]
          ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
          [color=blue][color=green][color=darkred]
          >>> [chr(x) for x in xrange(ord('A') , ord('Z') + 1)][/color][/color][/color]
          ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
          'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']

          etc...

          --
          Pedro Werneck

          Comment

          • Peter Otten

            #6
            Re: Character Sequence Generation

            Jeff Schwab wrote:
            [color=blue]
            > What's the best way to generate a sequence of characters in Python? I'm
            > looking for something like this Perl code: 'a' .. 'z' .[/color]
            [color=blue][color=green][color=darkred]
            >>> import re
            >>> def char_set(a_z, all_chars="".jo in(map(chr, range(256)))):[/color][/color][/color]
            .... return re.compile("[%s]" % a_z).findall(al l_chars)
            ....[color=blue][color=green][color=darkred]
            >>> for c in char_set("a-f0-9"): print c,[/color][/color][/color]
            ....
            0 1 2 3 4 5 6 7 8 9 a b c d e f[color=blue][color=green][color=darkred]
            >>> for c in char_set("\s"): print repr(c),[/color][/color][/color]
            ....
            '\t' '\n' '\x0b' '\x0c' '\r' ' '

            Peter

            Comment

            • Jeff Schwab

              #7
              Re: Character Sequence Generation

              Pedro Werneck wrote:[color=blue]
              > On Thu, 22 Sep 2005 23:26:58 -0400
              > Jeff Schwab <jeffrey.schwab @rcn.com> wrote:
              >
              >[color=green]
              >>What's the best way to generate a sequence of characters in Python?
              >>I'm looking for something like this Perl code: 'a' .. 'z' .[/color]
              >
              >
              > If you want arbitrary sequences, you may use something like:
              >
              >
              >[color=green][color=darkred]
              >>>>[chr(x) for x in xrange(ord('a') , ord('z') + 1)][/color][/color]
              >
              > ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
              > 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
              >
              >[color=green][color=darkred]
              >>>>[chr(x) for x in xrange(ord('d') , ord('p') + 1)][/color][/color]
              >
              > ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
              >
              >[color=green][color=darkred]
              >>>>[chr(x) for x in xrange(ord('A') , ord('Z') + 1)][/color][/color]
              >
              > ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',
              > 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
              >
              > etc...
              >[/color]

              Thanks, that's exactly what I want!

              Comment

              • skip@pobox.com

                #8
                Re: Sniffing Text Files


                David> I realize CSV module has a sniffer but it is something that is
                David> limited more or less to delimited files.

                Sure. How about:

                def sniff(fname):
                if open(fname).rea d(4) == "<xml":
                return "xml"
                else:
                # assume csv - use its sniffer to generate a dialect
                return d

                Of course, as the number of file formats grows, you'll need to expand the
                logic. You can also run the file(1) command and see what it says. I seem
                to recall someone asking about the equivalent to file(1) implemented in
                Python awhile back.

                --
                Skip Montanaro
                Katrina Benefit Concerts: http://www.musi-cal.com/katrina
                skip@pobox.com

                Comment

                Working...