473,378 Members | 1,384 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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' .
Sep 23 '05 #1
7 4904

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 '

Sep 23 '05 #2
me********@aol.com 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?
Sep 23 '05 #3

Jeff Schwab wrote:
me********@aol.com 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)])

Sep 23 '05 #4
On Thu, 22 Sep 2005 23:26:58 -0400
Jeff Schwab <je************@rcn.com> 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
Sep 23 '05 #5
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

Sep 23 '05 #6
Pedro Werneck wrote:
On Thu, 22 Sep 2005 23:26:58 -0400
Jeff Schwab <je************@rcn.com> 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!
Sep 23 '05 #7

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
sk**@pobox.com
Sep 23 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Michael . | last post by:
I had an error before involving a temporary table, and that has been taken care of... The last message I wrote where it seemed to have needed it after I added it was because of different...
9
by: Christian Kandeler | last post by:
Hi, if I want to store the string "123456" in a variable of type char, I can do it like this: char s = "123456"; Or like this: char s = { '1', '2', '3', '4', '5', '6', '\0' };
7
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %%...
6
by: Pavils Jurjans | last post by:
Hello, I am experiencing a weird behaviour on my ASP.NET project. The project consists from client-side, which can be whatever environment - web page, EXE application, etc. The client sends HTTP...
8
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
6
by: newtophp2000 | last post by:
Hello, Since SQL Server has no sequence generator, I wrote my own. (I claim no ownership of it as it is closely modeled after earlier discussions on this topic.) I have included the sql...
15
by: wizardyhnr | last post by:
i want to try ANSI C99's unicode fuctions. so i write a test program. the function is simple, but i cannot compile it with dev c++ 4.9.9.2 under windows xp sp2, since the compiler always think that...
37
by: Zhiv Kurilka | last post by:
Hi, I have a text file with following content: "((^)|(.* +))§§§§§§§§" if I read it with: k=System.IO.StreamReader( "file.txt",System.Text.Encoding.ASCII); k.readtotheend()
3
by: amija0311 | last post by:
Hi, I am new using DB2 9.1 database by windows base. I want to query the data that contain string then translate the string into integer using DB2. The problems is If the data is null, i got the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.