Re: (easy question) Find and replace multiple items

2006-08-08 Thread Mark Peters

ds4ff1z wrote:
> Hello, i'm looking to find and replace multiple characters in a text
> file (test1). I have a bunch of random numbers and i want to replace
> each number with a letter (such as replace a 7 with an f  and 6 with a
> d). I would like a suggestion on an a way to do this.  Thanks

how about:

>>> import string
>>> text1 = "foo bar 12 spam joe876"
>>> table = string.maketrans("0123456789","uydnwkdfpx")
>>> text1.translate(table)
'foo bar yd spam joepfd'

Mark Peters

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Unicode/ascii encoding nightmare

2006-11-06 Thread Mark Peters
> The string below is the encoding of the norwegian word "fødselsdag".
>
> >>> s = 'f\xc3\x83\xc2\xb8dselsdag'

I'm not sure which encoding method you used to get the string above.
Here's the result of my playing with the string in IDLE:

>>> u1 = u'fødselsdag'
>>> u1
u'f\xf8dselsdag'
>>> s1 = u1.encode('utf-8')
>>> s1
'f\xc3\xb8dselsdag'
>>> u2 = s1.decode('utf-8')
>>> u2
u'f\xf8dselsdag'
>>> print u2
fødselsdag
>>>

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Built-in Exceptions - How to Find Out Possible Errno's

2006-07-05 Thread Mark Peters
> I did find this but it doesn't have numbers and I can't tell if it's
> even what I'm looking for:
> http://docs.python.org/lib/module-errno.html

Error number picked at random:

>>> import errno
>>> print errno.errorcode.keys()
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 27, 28, 29, 30, 31, 32, 33, 34, 36, 38, 39, 40, 41, 42,
1, 10004, 10009, 10013, 10014, 10022, 10024, 10035, 10036, 10037,
10038, 10039, 10040, 10041, 10042, 10043, 10044, 10045, 10046, 10047,
10048, 10049, 10050, 10051, 10052, 10053, 10054, 10055, 10056, 10057,
10058, 10059, 10060, 10061, 10062, 10063, 10064, 10065, 10066, 10067,
10068, 10069, 10070, 10071, 10091, 10092, 10093, 10101]
>>> print errno.errorcode[10]
ECHILD

or

>>> import os
>>> print os.strerror(10)
No child processes

Hope this helps,
Mark Peters

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Built-in Exceptions - How to Find Out Possible Errno's

2006-07-05 Thread Mark Peters

Gregory Piñero wrote:
> Thanks Mark, that does help, but what is this errno module?  I mean,
> does it apply to OSError or to IOError or both?
 My guess is that the IOError will return the underlying operating
system error, but that's just a guess (and no time to dig for the
answer right now)

Mark

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Built-in Exceptions - How to Find Out Possible Errno's

2006-07-05 Thread Mark Peters
A quick test:

>>> try:
f = open("foo","r")
except IOError, error:
print errno.errorcode[error.errno]

ENOENT

It looks to me like your if statement should be as simple as:

if error.errno == errno.ENOENT:
print os.strerror(error.errno)

Mark

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Simple regex with whitespaces

2006-09-10 Thread Mark Peters
>   Which of course does not work. I cannot express the fact: sentence
> have 0 or 1 whitespace, separation of group have two or more
> whitespaces.
>
> Any suggestion ? Thanks a bunch !
How about this:

>>> import re
>>> s = 'hello world   how are you'
>>> re.split(r"\s{2,}",s)
['', 'hello world', 'how are you']

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Spliting a string on non alpha characters

2006-09-23 Thread Mark Peters
> I'm relatively new to python but I already noticed that many lines of
> python code can be simplified to a oneliner by some clever coder. As
> the topics says, I'm trying to split lines like this :
>
> 'foo bar- blah/hm.lala' -> [foo, bar, blah, hm, lala]
>
> 'foobbbar.. xyz' -> [foo, bbbar, xyz]
>
> obviously a for loop catching just chars could do the trick, but I'm
> looking for a more elegant way. Anyone can help?

A simple regular expression would work:
>>> import re
>>> s = 'foo bar- blah/hm.lala'
>>> re.findall(r"\w+",s)
['foo', 'bar', 'blah', 'hm', 'lala']

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python html rendering

2006-10-03 Thread Mark Peters
> Hi, Im looking for a way to display some python code
> in html: with correct indentation, possibly syntax hiliting, dealing
> correctly with multi-line comment, and... generating valid html code if
> the python code itself deals with html (hence manipulates tag litterals.
> Thanks for your help!

I haven't used it, but these seem promising:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/252170
http://silvercity.sourceforge.net/

Hope it helps.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: basic python questions

2006-11-18 Thread Mark Peters
[EMAIL PROTECTED] wrote:

> dict = {}

As a general rule you should avoid variable names which shadow built in
types (list, dict, etc.).  This can cause unexpected behavior later on.

Also, variable names should be more descriptive of their contents.

Try word_dict or some such variant

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: 6 Pick Bet Grouping

2006-11-28 Thread Mark Peters
> The totalizator system allows us to merge or group these four bets as
> follows:
>
> 5 + 7 / 3 / 11 / 7 + 14 / 1 / 9 - $50  ($200 total)
I'm still trying to get my head around what you're trying to do, but
here's some code:

---snip-
data = ["5 / 3 / 11 / 7 / 1 / 9  - $50",
"7 / 3 / 11 / 7 / 1 / 9  - $50",
"5 / 3 / 11 / 14 / 1 / 9  - $50",
"7 / 3 / 11 / 14 / 1 / 9  - $50"]

pick6 = [{},{},{},{},{},{}]

for line in data:
amount = int(line.split("-")[1].strip().lstrip("$"))
runners = [int(s) for s in line.split("-")[0].split("/")]
for x,r in enumerate(runners):
qty,amt = pick6[x].get(r,(0,0))
qty += 1
amt += amount
pick6[x][r] = (qty,amt)

for place in range(6):
print "For position ",place
for runner in sorted(pick6[place].keys()):
bets = pick6[place][runner][0]
betamt = pick6[place][runner][1]
print "  Runner",runner,"has",bets,"bets for $",betamt
---snip-
Output:

For position  0
Runner 5 has 2 bets for $ 100
Runner 7 has 2 bets for $ 100
For position  1
Runner 3 has 4 bets for $ 200
For position  2
Runner 11 has 4 bets for $ 200
For position  3
Runner 7 has 2 bets for $ 100
Runner 14 has 2 bets for $ 100
For position  4
Runner 1 has 4 bets for $ 200
For position  5
Runner 9 has 4 bets for $ 200

Is that anywhere close?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: TypeError: cannot concatenate 'str' and 'NoneType' objects

2006-12-20 Thread Mark Peters
> # Begin going through the loop
> Townshp = Townshps.next()
> while Townshps !="":
> #Set the output name to be the same as input
> outName = outputPath + "/" + Townshp + "land" + ".img"
> Output_table = outputPath + "/" + Townshp + "table" + ".dbf"
> #For each extract by Mask
> gp.ExtractbyMask_sa (raster, Townshp, outName)
> #For each tabluate area
> gp.TabulateArea_sa (Townshp, "RITOWN5K_", outName, "VALUE",
> Output_table, "98.425")
> Townshp = Townshps.next()

Warning: I know nothing about the Python ArcGIS stuff.

The first thing that jumps out at me is your while condition.  You are
testing "Townshps" when it seems from the code that you should be
testing "Townshp".  However, the typical Python way to iterate through
a list for be to use a for loop.
Perhaps replace the while statement with:
for Townshp in Townshps:
and remove the "Townshp = Townshps.next()" lines

If that doesn't do it, please post the entire traceback message that
you are seeing (copy and paste it) and it should tell us a lot more
about your error.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regex question

2007-01-08 Thread Mark Peters
> is there any way i would be successful then, in using raw string inside
> my makeRE() function?

Why do you think you even need a raw string?

Just build and return the string 'a|b|c' (NOTE: DON'T add the quotes to
the string)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: regex question

2007-01-08 Thread Mark Peters
> yes, i suppose you are right.  i can't think of a reason i would NEED a
> raw string in this situation.
It looks from your code that you are trying to remove all occurances of
one string from the other.  a simple regex way would be to use re.sub()

>>> import re
>>> a = "abc"
>>> b = "debcabbde"
>>> re.sub("[" + a + "]","",b)
'dede'

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Error message if there is a space in the source directory

2007-06-02 Thread Mark Peters
> When using a source like this on line 5:
>
> source = [r'C:\test\test 2\\']
>
> which has a space in the title, the program will not work.

Try wrapping that argument in double quotes when you build the command

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Probably simple syntax error

2007-07-01 Thread Mark Peters
> **weights_array(randomizing_counter) = small_randomized_int
>
> The starred line is the one getting the error message: "SyntaxError:
> can't assign to function call"
>
> Now, I do understand what this means. I'm trying to assign to a
> function instead of the value that the function should create. But
> since when is weights_array or small_randomizing_int a function? Both
> are being declared in the code on their first usage. This one has got
> me stumped, maybe you guys can help me out with it?

Using parens () indicates a function call.

I suspect things will work much better using square brackets [].

Square brackets indicate the index into a sequence (like a list)

-- 
http://mail.python.org/mailman/listinfo/python-list