Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Character Sequence Generation

Reply
Thread Tools

Character Sequence Generation

 
 
Jeff Schwab
Guest
Posts: n/a
 
      09-23-2005
What's the best way to generate a sequence of characters in Python? I'm
looking for something like this Perl code: 'a' .. 'z' .
 
Reply With Quote
 
 
 
 
mensanator@aol.com
Guest
Posts: n/a
 
      09-23-2005

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 = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ'
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
digits = '0123456789'
hexdigits = '0123456789abcdefABCDEF'
letters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv. ..\xcd\xce...
lowercase =
'abcdefghijklmnopqrstuvwxyz\x83\x9a\x9c\x9e\xaa\xb 5\xba\xd...
octdigits = '01234567'
printable =
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLM NOPQRSTU...
punctuation = '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
uppercase =
'ABCDEFGHIJKLMNOPQRSTUVWXYZ\x8a\x8c\x8e\x9f\xc0\xc 1\xc2\xc...
whitespace = '\t\n\x0b\x0c\r '

 
Reply With Quote
 
 
 
 
Jeff Schwab
Guest
Posts: n/a
 
      09-23-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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?
 
Reply With Quote
 
Devan L
Guest
Posts: n/a
 
      09-23-2005

Jeff Schwab wrote:
> (E-Mail Removed) 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)])

 
Reply With Quote
 
Pedro Werneck
Guest
Posts: n/a
 
      09-23-2005
On Thu, 22 Sep 2005 23:26:58 -0400
Jeff Schwab <(E-Mail Removed)> 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
 
Reply With Quote
 
Peter Otten
Guest
Posts: n/a
 
      09-23-2005
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

 
Reply With Quote
 
Jeff Schwab
Guest
Posts: n/a
 
      09-23-2005
Pedro Werneck wrote:
> On Thu, 22 Sep 2005 23:26:58 -0400
> Jeff Schwab <(E-Mail Removed)> 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!
 
Reply With Quote
 
skip@pobox.com
Guest
Posts: n/a
 
      09-23-2005

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
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to iterate over sequence and non-sequence ? stef mientki Python 13 10-20-2007 10:21 AM
character encoding +missing character sequence raavi Java 2 03-02-2006 05:01 AM
Cisco Pix 520(?) break character / sequence blazeking@gmail.com Cisco 5 10-17-2005 10:29 PM
BOOT SEQUENCE (how to change boot sequence) bird Computer Support 13 12-24-2003 02:20 AM
HTML Generation (Next Generation CGI) John W. Long Ruby 4 11-24-2003 04:24 AM



Advertisments