Re: why I don't like range/xrange
Roel Schroeven [EMAIL PROTECTED] on Sat, 17 Feb 2007 01:31:13 GMT didst step forth and proclaim thus: ... So, the point is that in C you can influence the loop's behavior by modifying the loop variable, while you cannot do that in Python (at least not in a for-loop). What's wrong with... for i in range(10): if condition: break ...? -- Sam Peterson skpeterson At nospam ucdavis.edu if programmers were paid to remove code instead of adding it, software would be much better -- unknown -- http://mail.python.org/mailman/listinfo/python-list
Re: Parsing HTML
mtuller [EMAIL PROTECTED] on 10 Feb 2007 15:03:36 -0800 didst step forth and proclaim thus: Alright. I have tried everything I can find, but am not getting anywhere. I have a web page that has data like this: [snip] What is show is only a small section. I want to extract the 33,699 (which is dynamic) and set the value to a variable so that I can insert it into a database. [snip] I have also tried Beautiful Soup, but had trouble understanding the documentation. from BeautifulSoup import BeautifulSoup as parser soup = parser(tr td headers=col1_1 style=width:21% span class=hpPageText LETTER/span/td td headers=col2_1 style=width:13%; text-align:right span class=hpPageText 33,699/span/td td headers=col3_1 style=width:13%; text-align:right span class=hpPageText 1.0/span/td td headers=col4_1 style=width:13%; text-align:right /tr) value = \ int(soup.find('td', headers='col2_1').span.contents[0].replace(',', '')) Thanks, Mike Hope that helped. This code assumes there aren't any td tags with header=col2_1 that come before the value you are trying to extract. There's several ways to do things in BeautifulSoup. You should play around with BeautifulSoup in the interactive prompt. It's simply awesome if you don't need speed on your side. -- Sam Peterson skpeterson At nospam ucdavis.edu if programmers were paid to remove code instead of adding it, software would be much better -- unknown -- http://mail.python.org/mailman/listinfo/python-list
Re: How to find all the same words in a text?
Johny [EMAIL PROTECTED] on 10 Feb 2007 05:29:23 -0800 didst step forth and proclaim thus: I need to find all the same words in a text . What would be the best idea to do that? I make no claims of this being the best approach: def findOccurances(a_string, word): Given a string and a word, returns a double: [0] = count [1] = list of indexes where word occurs import re count = 0 indexes = [] start = 0 # offset for successive passes pattern = re.compile(r'\b%s\b' % word, re.I) while True: match = pattern.search(a_string) if not match: break count += 1; indexes.append(match.start() + start) start += match.end() a_string = a_string[match.end():] return (count, indexes) Seems to work for me. No guarantees. -- Sam Peterson skpeterson At nospam ucdavis.edu if programmers were paid to remove code instead of adding it, software would be much better -- unknown -- http://mail.python.org/mailman/listinfo/python-list
Re: How to find all the same words in a text?
[EMAIL PROTECTED] on 11 Feb 2007 08:16:11 -0800 didst step forth and proclaim thus: More concisely: import re pattern = re.compile(r'\b324\b') indices = [ match.start() for match in pattern.finditer(target_string) ] print Indices, indices print Count: , len(indices) Thank you, this is educational. I didn't realize that finditer returned match objects instead of tuples. Cheers, Steven -- Sam Peterson skpeterson At nospam ucdavis.edu if programmers were paid to remove code instead of adding it, software would be much better -- unknown -- http://mail.python.org/mailman/listinfo/python-list
Re: No module named pyExcelerator Error
susan [EMAIL PROTECTED] on 11 Feb 2007 16:55:35 -0800 didst step forth and proclaim thus: Hi, I'm new of Python, and this problem stucked me whole day but can't be solved. [snip] anybody can tell me where's wrong please? Thanks in advance! What are the contents of sys.path from an interactive prompt? Have you tried the official windows Python? Is there a reason you need to use the cygwin Python? -- Sam Peterson skpeterson At nospam ucdavis.edu if programmers were paid to remove code instead of adding it, software would be much better -- unknown -- http://mail.python.org/mailman/listinfo/python-list
string.replace non-ascii characters
Greetings Pythonistas. I have recently discovered a strange anomoly with string.replace. It seemingly, randomly does not deal with characters of ordinal value 127. I ran into this problem while downloading auction web pages from ebay and trying to replace the \xa0 (dec 160, nbsp char in iso-8859-1) in the string I got from urllib2. Yet today, all is fine, no problems whatsoever. Sadly, I did not save the exact error message, but I believe it was a ValueError thrown on string.replace and the message was something to the effect character value not within range(128). Some googling seemed to indicate other people have reported similar troubles: http://mail.python.org/pipermail/python-list/2006-July/391617.html Anyone have any enlightening advice for me? -- Sam Peterson skpeterson At nospam ucdavis.edu if programmers were paid to remove code instead of adding it, software would be much better -- unknown -- http://mail.python.org/mailman/listinfo/python-list
Re: About getattr()
Jm lists [EMAIL PROTECTED] on Mon, 12 Feb 2007 12:36:10 +0800 didst step forth and proclaim thus: Hello, Since I can write the statement like: print os.path.isdir.__doc__ Test whether a path is a directory Why do I still need the getattr() func as below? print getattr(os.path,isdir).__doc__ Test whether a path is a directory getattr lets you lookup an attribute given a string, so the attribute wouldn't have to be hardcoded in your program, it could come from a file, or from user input. -- Sam Peterson skpeterson At nospam ucdavis.edu if programmers were paid to remove code instead of adding it, software would be much better -- unknown -- http://mail.python.org/mailman/listinfo/python-list
Re: searching a list of lists as a two-dimensional array?
James Stroud [EMAIL PROTECTED] on Sun, 11 Feb 2007 16:53:16 -0800 didst step forth and proclaim thus: agent-s wrote: Basically I'm programming a board game and I have to use a list of lists to represent the board (a list of 8 lists with 8 elements each). I have to search the adjacent cells for existing pieces and I was wondering how I would go about doing this efficiently. Thanks This isn't very clear. What do you mean by I have to search the adjacent cells for existing pieces? If piece is 1 and empty is 0 and piece is at ary[row][col]: import operator srch = [(i,j) for i in [-1,0,1] for j in [-1,0,1] if (i,j) != (0,0)] is_adj = reduce(operator.or_, [ary[row+i][col+j] for (i,j) in srch]]) Wow, maybe it's just me (I'm a pretty bad programmer) but this is where list comprehensions begin to look unreadable to me. Here's a C-like way to do it, (warning, untested in python): for i in range(8): for j in range(8): for offset_i in range(-1,2): for offset_j in range(-1, 2): row = i + offset_i col = j + offset_j if (row 0 or row 7) or (col 0 or col 8) \ or ((row,col) == (i,j)): continue # else do something with board[row][col] I realize this is gross and un-Pythonic and does the same thing the above code does, but it's probably the way I'd choose to do it :). Then again, I've been negatively influenced by doing a game of life in C a few months back. -- Sam Peterson skpeterson At nospam ucdavis.edu if programmers were paid to remove code instead of adding it, software would be much better -- unknown -- http://mail.python.org/mailman/listinfo/python-list
Re: string.replace non-ascii characters
Steven Bethard [EMAIL PROTECTED] on Sun, 11 Feb 2007 22:23:59 -0700 didst step forth and proclaim thus: Samuel Karl Peterson wrote: Greetings Pythonistas. I have recently discovered a strange anomoly with string.replace. It seemingly, randomly does not deal with characters of ordinal value 127. I ran into this problem while downloading auction web pages from ebay and trying to replace the \xa0 (dec 160, nbsp char in iso-8859-1) in the string I got from urllib2. Yet today, all is fine, no problems whatsoever. Sadly, I did not save the exact error message, but I believe it was a ValueError thrown on string.replace and the message was something to the effect character value not within range(128). Was it something like this? u'\xa0'.replace('\xa0', '') Traceback (most recent call last): File interactive input, line 1, in module UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 0: ordinal not in range(128) Yeah that looks like exactly what was happening, thank you. I wonder why I had a unicode string though. I thought urllib2 always spat out a plain string. Oh well. u'\xa0'.encode('latin-1').replace('\xa0', ) Horray. -- Sam Peterson skpeterson At nospam ucdavis.edu if programmers were paid to remove code instead of adding it, software would be much better -- unknown -- http://mail.python.org/mailman/listinfo/python-list