Character Sequence Generation

Discussion in 'Python' started by Jeff Schwab, Sep 23, 2005.

  1. Jeff Schwab

    Jeff Schwab Guest

    What's the best way to generate a sequence of characters in Python? I'm
    looking for something like this Perl code: 'a' .. 'z' .
    Jeff Schwab, Sep 23, 2005
    #1
    1. Advertising

  2. Jeff Schwab

    Guest

    Jeff Schwab wrote:
    > What's the best way to generate a sequence of characters in Python? I'm
    > looking for something like this Perl code: 'a' .. 'z' .


    >>> import string


    >>> print string.ascii_lowercase

    abcdefghijklmnopqrstuvwxyz


    Others:

    ascii_letters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
    ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
    ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
    digits = '0123456789'
    hexdigits = '0123456789abcdefABCDEF'
    letters =
    'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv...\xcd\xce...
    lowercase =
    'abcdefghijklmnopqrstuvwxyz\x83\x9a\x9c\x9e\xaa\xb5\xba\xd...
    octdigits = '01234567'
    printable =
    '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTU...
    punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
    uppercase =
    'ABCDEFGHIJKLMNOPQRSTUVWXYZ\x8a\x8c\x8e\x9f\xc0\xc1\xc2\xc...
    whitespace = '\t\n\x0b\x0c\r '
    , Sep 23, 2005
    #2
    1. Advertising

  3. Jeff Schwab

    Jeff Schwab Guest

    wrote:
    > Jeff Schwab wrote:
    >
    >>What's the best way to generate a sequence of characters in Python? I'm
    >>looking for something like this Perl code: 'a' .. 'z' .

    >
    >
    >>>>import string

    >
    >
    >>>>print string.ascii_lowercase

    >
    > abcdefghijklmnopqrstuvwxyz


    Thanks. Is there a good way to generate the characters dynamically?
    Jeff Schwab, Sep 23, 2005
    #3
  4. Jeff Schwab

    Devan L Guest

    Jeff Schwab wrote:
    > wrote:
    > > Jeff Schwab wrote:
    > >
    > >>What's the best way to generate a sequence of characters in Python? I'm
    > >>looking for something like this Perl code: 'a' .. 'z' .

    > >
    > >
    > >>>>import string

    > >
    > >
    > >>>>print string.ascii_lowercase

    > >
    > > abcdefghijklmnopqrstuvwxyz

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


    ''.join([chr(i) for i in range(97,123)])
    Devan L, Sep 23, 2005
    #4
  5. On Thu, 22 Sep 2005 23:26:58 -0400
    Jeff Schwab <> wrote:

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


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


    >>> [chr(x) for x in xrange(ord('a'), ord('z') + 1)]

    ['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']

    >>> [chr(x) for x in xrange(ord('d'), ord('p') + 1)]

    ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']

    >>> [chr(x) for x in xrange(ord('A'), ord('Z') + 1)]

    ['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
    Pedro Werneck, Sep 23, 2005
    #5
  6. Jeff Schwab

    Peter Otten Guest

    Jeff Schwab wrote:

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


    >>> import re
    >>> def char_set(a_z, all_chars="".join(map(chr, range(256)))):

    .... return re.compile("[%s]" % a_z).findall(all_chars)
    ....
    >>> for c in char_set("a-f0-9"): print c,

    ....
    0 1 2 3 4 5 6 7 8 9 a b c d e f
    >>> for c in char_set("\s"): print repr(c),

    ....
    '\t' '\n' '\x0b' '\x0c' '\r' ' '

    Peter
    Peter Otten, Sep 23, 2005
    #6
  7. Jeff Schwab

    Jeff Schwab Guest

    Pedro Werneck wrote:
    > On Thu, 22 Sep 2005 23:26:58 -0400
    > Jeff Schwab <> wrote:
    >
    >
    >>What's the best way to generate a sequence of characters in Python?
    >>I'm looking for something like this Perl code: 'a' .. 'z' .

    >
    >
    > If you want arbitrary sequences, you may use something like:
    >
    >
    >
    >>>>[chr(x) for x in xrange(ord('a'), ord('z') + 1)]

    >
    > ['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']
    >
    >
    >>>>[chr(x) for x in xrange(ord('d'), ord('p') + 1)]

    >
    > ['d', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p']
    >
    >
    >>>>[chr(x) for x in xrange(ord('A'), ord('Z') + 1)]

    >
    > ['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...
    >


    Thanks, that's exactly what I want!
    Jeff Schwab, Sep 23, 2005
    #7
  8. Jeff Schwab

    Guest

    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).read(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
    , Sep 23, 2005
    #8
    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. raavi
    Replies:
    2
    Views:
    910
    raavi
    Mar 2, 2006
  2. Kenneth McDonald
    Replies:
    1
    Views:
    835
    Carl Banks
    Dec 27, 2006
  3. stef mientki
    Replies:
    13
    Views:
    634
    stef mientki
    Oct 20, 2007
  4. John W. Long

    HTML Generation (Next Generation CGI)

    John W. Long, Nov 22, 2003, in forum: Ruby
    Replies:
    4
    Views:
    331
    John W. Long
    Nov 24, 2003
  5. Replies:
    11
    Views:
    272
    Raul Parolari
    Dec 2, 2007
Loading...

Share This Page