From: timo verbeek on 15 May 2010 07:02 I'm planning to create a human word program A human inputs a string "Give me the weather for London please." Then I will strip the string. "weather for london" Then I get the useful information. what:"weather" where:"london" After that I use the info. I need help with getting the useful information how do I get the place if I don't now how long the string is?
From: timo verbeek on 15 May 2010 07:32 On May 15, 1:02 pm, timo verbeek <timoverbee...(a)gmail.com> wrote: Place starts always with for
From: superpollo on 15 May 2010 08:35 timo verbeek ha scritto: > I'm planning to create a human word program > A human inputs a string > "Give me the weather for London please." > Then I will strip the string. > "weather for london" > Then I get the useful information. > what:"weather" where:"london" > After that I use the info. > > I need help with getting the useful information how do I get the place > if I don't now how long the string is? >>> query = "Give me the weather for London please." >>> what = query.strip("Give me the ").split()[0] >>> where = query.strip("Give me the " + what + " for ").split()[0] >>> print what, where weather London >>>
From: superpollo on 15 May 2010 08:55 superpollo ha scritto: > timo verbeek ha scritto: >> I'm planning to create a human word program >> A human inputs a string >> "Give me the weather for London please." >> Then I will strip the string. >> "weather for london" >> Then I get the useful information. >> what:"weather" where:"london" >> After that I use the info. >> >> I need help with getting the useful information how do I get the place >> if I don't now how long the string is? > > > >>> query = "Give me the weather for London please." > >>> what = query.strip("Give me the ").split()[0] > >>> where = query.strip("Give me the " + what + " for ").split()[0] > >>> print what, where > weather London > >>> maybe better not with strip, fot it might not do what i intended (see docs); maybe preferable to use "partition" method: >>> query = "Give me the weather for London please." >>> what = query.partition("Give me the ")[2].split()[0] >>> where = query.partition(" for ")[2].split()[0] >>> print what, where weather London >>>
From: David Zaslavsky on 15 May 2010 17:13 Here's my take on that: loc = re.search('for\s+(\w+)', string).group(1) Not much different, really, but it does allow for multiple spaces (\s+) as well as requiring at least one character in the word (\w+), and I use a matching group to extract the location directly instead of splitting the string "by hand". :) David On Saturday 15 May 2010 8:38:01 am Xavier Ho wrote: > On Sat, May 15, 2010 at 9:32 PM, timo verbeek <timoverbeek10(a)gmail.com>wrote: > > On May 15, 1:02 pm, timo verbeek <timoverbee...(a)gmail.com> wrote: > > Place starts always with for > > Okay, much better. > > Given that constraint, it looks like regular expression can do the job. I'm > not very experienced with regex, though. > > \w* matches a whole word composed of letters and numbers by default. > > >>> result = re.search('for \w*', 'Give me the weather for London please.') > >>> result.group() > > 'for London' > > >>> result.group().split()[1] > > 'London' > > Cheers, > Xav
|
Next
|
Last
Pages: 1 2 Prev: yappi 0.51 released Next: Pyinstaller on WINE - cannot install pywin32 |