Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99598

Re: reading from a txt file

From Gary Herron <gherron@digipen.edu>
Newsgroups comp.lang.python
Subject Re: reading from a txt file
Date 2015-11-26 13:01 -0800
Message-ID <mailman.154.1448572207.20593.python-list@python.org> (permalink)
References <175ab5d2-d8e3-44e7-a71f-88b3153daf89@googlegroups.com>

Show all headers | View raw


On 11/26/2015 12:34 PM, vincentypedro@gmail.com wrote:
> Hey, I'm wondering how to read individual strings in a text file.  I can read a text file by lines with .readlines() ,
> but I need to read specifically by strings, not including spaces.  Thanks in advance

Read the lines with readlines(), as you say, then split each line into 
whatever pieces you want with split (or with any of the many other 
string methods).

A minimal example (without readlines):
 >>> lines = ['a b c', 'aa bb cc']
 >>> for line in lines:
...   words = line.split()
...   print(words)
...
['a', 'b', 'c']
['aa', 'bb', 'cc']


-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

reading from a txt file vincentypedro@gmail.com - 2015-11-26 12:34 -0800
  Re: reading from a txt file Denis McMahon <denismfmcmahon@gmail.com> - 2015-11-26 20:50 +0000
  Re: reading from a txt file Jason Friedman <jsf80238@gmail.com> - 2015-11-26 14:04 -0700
  Re: reading from a txt file Gary Herron <gherron@digipen.edu> - 2015-11-26 13:01 -0800
  Re: reading from a txt file Pedro Vincenty <vincentypedro@gmail.com> - 2015-11-26 17:24 -0800
    Re: reading from a txt file Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2015-11-26 21:44 -0500

csiph-web