re beginner
faulkner
faulkner612 at comcast.net
Sun Jun 4 16:55:44 EDT 2006
you could write a function which takes a match object and modifies d,
pass the function to re.sub, and ignore what re.sub returns.
# untested code
d = {}
def record(match):
s = match.string[match.start() : match.end()]
i = s.index('\t')
print s, i # debugging
d[s[:i]] = int(s[i+1:])
return ''
re.sub('\w+\t\d+\t', record, stuff)
# end code
it may be a bit faster, but it's very roundabout and difficult to
debug.
SuperHik wrote:
> hi all,
>
> I'm trying to understand regex for the first time, and it would be very
> helpful to get an example. I have an old(er) script with the following
> task - takes a string I copy-pasted and wich always has the same format:
>
> >>> print stuff
> Yellow hat 2 Blue shirt 1
> White socks 4 Green pants 1
> Blue bag 4 Nice perfume 3
> Wrist watch 7 Mobile phone 4
> Wireless cord! 2 Building tools 3
> One for the money 7 Two for the show 4
>
> >>> stuff
> 'Yellow hat\t2\tBlue shirt\t1\nWhite socks\t4\tGreen pants\t1\nBlue
> bag\t4\tNice perfume\t3\nWrist watch\t7\tMobile phone\t4\nWireless
> cord!\t2\tBuilding tools\t3\nOne for the money\t7\tTwo for the show\t4'
>
> I want to put items from stuff into a dict like this:
> >>> print mydict
> {'Wireless cord!': 2, 'Green pants': 1, 'Blue shirt': 1, 'White socks':
> 4, 'Mobile phone': 4, 'Two for the show': 4, 'One for the money': 7,
> 'Blue bag': 4, 'Wrist watch': 7, 'Nice perfume': 3, 'Yellow hat': 2,
> 'Building tools': 3}
>
> Here's how I did it:
> >>> def putindict(items):
> ... items = items.replace('\n', '\t')
> ... items = items.split('\t')
> ... d = {}
> ... for x in xrange( len(items) ):
> ... if not items[x].isdigit(): d[items[x]] = int(items[x+1])
> ... return d
> >>>
> >>> mydict = putindict(stuff)
>
>
> I was wondering is there a better way to do it using re module?
> perheps even avoiding this for loop?
>
> thanks!
More information about the Python-list
mailing list