473,511 Members | 16,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[re.finditer] Getting all occurences in one go?

Hello

I'd like to make sure there isn't an easier way to extract all the
occurences found with re.finditer:

=======================
req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req).read()
matches = re.compile("(\d+).html").finditer(response)
# ----------- BEGIN
for match in matches:
if mytable[item]=="":
mytable[item]= match.group(1)
else:
mytable[item]= mytable[item] + "," + match.group(1) #
----------- END
=======================

Can the lines between BEGIN/END be simplified so I can copy all the
items into the mytable[] dictionary in one go, instead of getting each
one in a row, and append them with a comma, eg.

# I want this : mytable[123] = 6548,8457,etc."
# DOESN' T WORK
# mytable[item] = matches.group(0)
mytable[item] = matches.getall()

Thank you.
Apr 22 '07 #1
3 1818
On Sun, 22 Apr 2007 23:28:23 +0200, Gilles Ganault <no****@nospam.com>
wrote:
>I'd like to make sure there isn't an easier way to extract all the
occurences found with re.finditer:
Oops, s/match/item/:

req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req).read()
matches = re.compile("(\d+).html").finditer(response)
# ----------- BEGIN
for item in matches:
if mytable[item]=="":
mytable[item]= match.group(1)
else:
mytable[item]= mytable[item] + "," + match.group(1) #
----------- END
Apr 22 '07 #2
Gilles Ganault <no****@nospam.comwrites:
for match in matches:
if mytable[item]=="":
mytable[item]= match.group(1)
else:
mytable[item]= mytable[item] + "," + match.group(1) #
----------- END
=======================

Can the lines between BEGIN/END be simplified so I can copy all the
items into the mytable[] dictionary in one go, instead of getting each
one in a row, and append them with a comma, eg.
Yes, look at the string.join method and generator expressions. You'd
say:

mytable[item] = ','.join(m.group(1) for m in matches)
Apr 22 '07 #3
On 22 Apr 2007 15:33:37 -0700, Paul Rubin
<http://ph****@NOSPAM.invalidwrote:
mytable[item] = ','.join(m.group(1) for m in matches)
Thanks, that did it.
Apr 23 '07 #4

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

Similar topics

0
1369
by: Robert Oschler | last post by:
I figured I'd utter some words of praise for re.finditer(). It's such a great feeling, when you come across a single statement that does everything you want in two lines of code: for g in...
4
7631
by: Erik Johnson | last post by:
I am still fairly new to Python and trying to learn to put RE's to good use. I am a little confused about the finditer() method. It is documented like so: finditer( pattern, string) Return an...
8
2856
by: Chris Lasher | last post by:
Hello, I really like the finditer() method of the re module. I'm having difficulty at the moment, however, because finditer() still creates a callable-iterator oject, even when no match is found....
6
3144
by: Erick | last post by:
Hello, I've been looking for a while for an answer, but so far I haven't been able to turn anything up yet. Basically, what I'd like to do is to use re.finditer to search a large file (or a file...
4
13629
by: Jason Gleason | last post by:
What's the most efficient way to get the number of occurences of a certain string in another string..for instance i'm using the following code right now... private int CharacterCounter(String...
4
3352
by: Dameon | last post by:
Hi All, I have a process where I'd like to search the contents of a file(in a dir) for all occurences (or the count of) of a given string. My goal is to focus more on performance, as some of the...
8
5600
by: Daneel | last post by:
Hello! I'm looking for an algorithm which finds all occurences of a bit sequence (e.g., "0001") in a file. This sequence can start at any bit in the file (it is not byte aligned). I have some...
1
4109
ajhayes
by: ajhayes | last post by:
I posted a few weeks ago about trying to set up a way to order by unit number (http://bytes.com/topic/access/answers/864507-need-help-setting-up-table-structure-ordering-unit) I'm trying to use a...
3
2463
by: SLauren | last post by:
Hi, I am trying to find the records based on a search string which can be anything and can match any of the varchar fields in the table. Right now i am getting those records by using the...
0
7138
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7353
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7508
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5662
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5063
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4737
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1572
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
446
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.