Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Gary Herron Newsgroups: comp.lang.python Subject: Re: reading from a txt file Date: Thu, 26 Nov 2015 13:01:55 -0800 Lines: 24 Message-ID: References: <175ab5d2-d8e3-44e7-a71f-88b3153daf89@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de aFYTkG4vl+IimEF0I0lvWQ8AONebxlgu4x4+5x/QL1BA== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.025 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'subject:file': 0.07; '(without': 0.09; 'lines:': 0.09; 'advance': 0.10; "'b',": 0.16; "['a": 0.16; "['a',": 0.16; 'line.split()': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:txt': 0.16; 'wrote:': 0.16; 'string': 0.17; 'say,': 0.18; '>>>': 0.20; 'file.': 0.22; '(or': 0.23; 'split': 0.23; 'words': 0.24; 'header:In-Reply-To:1': 0.24; 'wondering': 0.25; 'header:User-Agent:1': 0.26; 'example': 0.26; 'pieces': 0.27; 'specifically': 0.28; 'strings,': 0.29; "i'm": 0.30; 'minimal': 0.30; 'file': 0.34; 'text': 0.35; 'but': 0.36; 'lines': 0.36; 'to:addr:python-list': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'thanks': 0.37; 'whatever': 0.39; 'subject:from': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'email addr:gmail.com': 0.62; 'charset:windows-1252': 0.62; 'dr.': 0.69; 'hey,': 0.75; 'received:204': 0.75; 'institute': 0.77; '(425)': 0.84; '895-4418': 0.84; 'digipen': 0.84; 'herron': 0.84 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: <175ab5d2-d8e3-44e7-a71f-88b3153daf89@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99598 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