i want to assign a xter to this, is, a, python , coding and group
thanks
thanks brother
i mean how do i particularly assign (u = this)
(y = is)....
in the strings up there. i have been able to split strings with any
character sign.
If i'm not wrong this is simple with RE:
In [1]: st = 'this is a python coding group'
In [2]: import re
In [3]: re.compile( "(?P<first>.*) (?P<second>.*) (?P<t>.*) (?P<fo>.*)
(?P<fi>.*) (?P<si>.*)" )
Out[3]: <_sre.SRE_Pattern object at 0x9e93ac0>
In [4]: rule = re.compile( "(?P<first>.*) (?P<second>.*) (?P<t>.*)
(?P<fo>.*) (?P<fi>.*) (?P<si>.*)" )
In [5]: m = rule.match( st )
In [6]: dir(m)
Out[6]:
['__copy__', '__deepcopy__',
'end', 'expand',
'group', 'groupdict',
'groups', 'span', 'start']
In [7]: m.groupdict().items()
Out[7]:
[('si', 'group'),
('second', 'is'),
('t', 'a'),
('fi', 'coding'),
('fo', 'python'),
('first', 'this')]
In [8]: dict(m.groupdict().items())
Out[8]:
{'fi': 'coding',
'first': 'this',
'fo': 'python',
'second': 'is',
'si': 'group',
't': 'a'}
Glauco
u, v, w, x, y, z = 'this is a python coding group'.split()
But if you have a variable number of words this isn't practical in
Python 2, though Python 3 has features that make it easier.
The real question is "what is the larger goal you are trying to
achieve". Where a programmer is trying to create names dynamically there
are usually better ways to proceed. Could you tell us a little more
about what you are trying to do?
regards
Steve
--
Steve Holden +1 571 484 6266 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/
>> thanks brother
>> i mean how do i particularly assign (u = this)
>> (y = is)....
>> in the strings up there. i have been able to split strings with any
>> character sign.
>>
>>
>
> If i'm not wrong this is simple with RE:
>
Using Regular Expression for this is an overkill, you'd better use the
str.split:
longstring = 'this is a python string'
splitted_string = longstring.split()
result = ', '.join(splitted_string[:-1]) + ' and ' + splitted_string[-1]
>> thanks brother
>> i mean how do i particularly assign (u = this)
>> (y = is)....
>> in the strings up there. i have been able to split strings with any
>> character sign.
>>
>>
>
> If i'm not wrong this is simple with RE:
If that's your idea of "simple", I'd hate to see what you consider
complicated!
*Simple* is just using the split method.
a, b, c, d, e, f = 'this is a python coding group'.split()
--
Steven
I've done a lot of file import procedure and IMHO this solution help you
in all situation. You can validate line before import, you can do a
specific RE for check a more detailed line and so on, it's more powerful.
For simple i mean simple programming code.
Glauco