Character Sequence Generation

J

Jeff Schwab

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

mensanator

Jeff said:
What's the best way to generate a sequence of characters in Python? I'm
looking for something like this Perl code: 'a' .. 'z' .
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 '
 
P

Pedro Werneck

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...
 
P

Peter Otten

Jeff said:
What's the best way to generate a sequence of characters in Python? I'm
looking for something like this Perl code: 'a' .. 'z' .
.... return re.compile("[%s]" % a_z).findall(all_chars)
........
0 1 2 3 4 5 6 7 8 9 a b c d e f....
'\t' '\n' '\x0b' '\x0c' '\r' ' '

Peter
 
J

Jeff Schwab

Pedro said:
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!
 
S

skip

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,539
Members
45,024
Latest member
ARDU_PROgrammER

Latest Threads

Top