By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,818 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,818 IT Pros & Developers. It's quick & easy.

getting words from readline

P: n/a

How is best to extract word strings from a
line = infile.readline()

I wish to quickly check the first word of
each line of a text file.

Indeed, How do I break a lineinput() line
into component words (separate by spaces etc) ?

Should I be looking at;
Re Parser Slice StringIO ?
Any help appreciated.


Sep 11 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi !

Look : .split()

@-salutations

Michel Claveau
Sep 11 '05 #2

P: n/a
Adam wrote:
How is best to extract word strings from a
line = infile.readline()

I wish to quickly check the first word of
each line of a text file.

Indeed, How do I break a lineinput() line
into component words (separate by spaces etc) ?

Should I be looking at;
Re Parser Slice StringIO ?
Any help appreciated.

Python 2.4.1 (#1, May 27 2005, 18:02:40)
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
s = "This is a line of text"
s.split() ['This', 'is', 'a', 'line', 'of', 'text'] l = s.split()
l[0] 'This'


As you can see above, you probably don't need any complicated modules
for this fairly simple requirement. If you are wanting to match the
words against each other, or against some pre-exisitng list, you might
want to reduce the input to lower-case first.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC http://www.holdenweb.com/

Sep 11 '05 #3

P: n/a

Ouch. That is simple with .split

I must have got confused amongst
slice re etc. searching searching.

Thanks folks. Painless.
Sep 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.