Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

string in files

1 view
Skip to first unread message

ibp...@gmail.com

unread,
Dec 30, 2008, 5:12:41 AM12/30/08
to
guys i need info on how to call up different words in a line of a file
using python
example : file = 'this is a python coding group'

i want to assign a xter to this, is, a, python , coding and group

thanks

Narasimhan Raghu-RBQG84

unread,
Dec 30, 2008, 5:17:24 AM12/30/08
to ibp...@gmail.com, pytho...@python.org
Simple solution: us result=yourString.split(" ") and you get a list with
all the words.

thanks
--
http://mail.python.org/mailman/listinfo/python-list

ibp...@gmail.com

unread,
Dec 30, 2008, 5:31:21 AM12/30/08
to
On Dec 30, 11:17 am, "Narasimhan Raghu-RBQG84" <RBQ...@motorola.com>
wrote:

> Simple solution: us result=yourString.split(" ") and you get a list with
> all the words.
>
> -----Original Message-----
> From: python-list-bounces+rbqg84=motorola....@python.org
>
> [mailto:python-list-bounces+rbqg84=motorola....@python.org] On Behalf Of
> ibpe...@gmail.com

> Sent: Tuesday, December 30, 2008 3:43 PM
> To: python-l...@python.org

> Subject: string in files
>
> guys i need info on how to call up different words in a line of a file
> using python example : file = 'this is a python coding group'
>
> i want to assign a xter to this, is, a, python , coding and group
>
> thanks
> --http://mail.python.org/mailman/listinfo/python-list
>
>

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.

Glauco

unread,
Dec 30, 2008, 5:53:17 AM12/30/08
to
>
> 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

Steve Holden

unread,
Dec 30, 2008, 8:02:11 AM12/30/08
to pytho...@python.org
ibp...@gmail.com wrote:
> On Dec 30, 11:17 am, "Narasimhan Raghu-RBQG84" <RBQ...@motorola.com>
> wrote:
>> Simple solution: us result=yourString.split(" ") and you get a list with
>> all the words.
>>
>> -----Original Message-----
>> From: python-list-bounces+rbqg84=motorola....@python.org
>>
>> [mailto:python-list-bounces+rbqg84=motorola....@python.org] On Behalf Of
>> ibpe...@gmail.com
>> Sent: Tuesday, December 30, 2008 3:43 PM
>> To: python-l...@python.org
>> Subject: string in files
>>
>> guys i need info on how to call up different words in a line of a file
>> using python example : file = 'this is a python coding group'
>>
>> i want to assign a xter to this, is, a, python , coding and group
>>
[...]

> 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.
>
Well, if you *know* you have a particular number of words in the string
you can say

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/

Lie Ryan

unread,
Dec 30, 2008, 8:24:18 AM12/30/08
to pytho...@python.org
On Tue, 30 Dec 2008 11:53:17 +0100, Glauco wrote:


>> 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]

Steven D'Aprano

unread,
Dec 30, 2008, 8:16:24 PM12/30/08
to
On Tue, 30 Dec 2008 11:53:17 +0100, Glauco wrote:


>> 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

Glauco

unread,
Dec 31, 2008, 3:29:19 AM12/31/08
to
Steven D'Aprano ha scritto:

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

0 new messages