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


Groups > comp.lang.python > #75303

Re: reading text files with indentation

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <gary.herron@islandtraining.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'subject:text': 0.05; 'that?': 0.05; 'removes': 0.07; 'lines:': 0.09; 'lost.': 0.09; 'received:67.192': 0.09; 'received:67.192.241': 0.09; 'received:dfw.emailsrvr.com': 0.09; 'subject:files': 0.09; 'cheers': 0.12; '"r")': 0.16; 'files:': 0.16; 'line)': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '&gt;&gt;&gt;': 0.24; 'received:emailsrvr.com': 0.24; 'file.': 0.24; 'received:(smtp server)': 0.26; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; '"",': 0.31; 'file:': 0.31; 'gary': 0.31; 'indentation': 0.31; 'strip': 0.31; 'file': 0.32; 'text': 0.33; 'open': 0.33; 'there,': 0.34; 'subject:with': 0.35; 'there': 0.35; 'method': 0.36; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'how': 0.40; 'read': 0.60; 'abc': 0.84
X-Virus-Scanned OK
X-Sender-Id gary.herron@islandtraining.com
Date Mon, 28 Jul 2014 00:39:51 -0700
From Gary Herron <gary.herron@islandtraining.com>
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Thunderbird/24.6.0
MIME-Version 1.0
To python-list@python.org
Subject Re: reading text files with indentation
References <53D5ECD0.8010702@enabled.com>
In-Reply-To <53D5ECD0.8010702@enabled.com>
Content-Type multipart/alternative; boundary="------------080307010602070803030203"
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.12381.1406533604.18130.python-list@python.org> (permalink)
Lines 99
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1406533604 news.xs4all.nl 2888 [2001:888:2000:d::a6]:36020
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:75303

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 07/27/2014 11:25 PM, Noah wrote:
> Hi there,
>
> The following code I am using to read in lines from a text file. The 
> indentation of the text is getting lost.  How can I correct that?
>
>
>     for file in files:
>           with open (file, "r") as file:
>         lines = file.readlines()
>
>     for line in lines:
>             line = re.sub("#.*", "", line)
>             line = line.strip()

The *strip* method on strings removes all whitespace from both ends.  
There goes your indentation.

> policy_lines.append(line)
>             print line
>
> Cheers

Example:

 >>> "   abc   ".strip()
'abc'


Gary Herron

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


Thread

Re: reading text files with indentation Gary Herron <gary.herron@islandtraining.com> - 2014-07-28 00:39 -0700

csiph-web