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


Groups > comp.lang.python > #75303 > unrolled thread

Re: reading text files with indentation

Started byGary Herron <gary.herron@islandtraining.com>
First post2014-07-28 00:39 -0700
Last post2014-07-28 00:39 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#75303 — Re: reading text files with indentation

FromGary Herron <gary.herron@islandtraining.com>
Date2014-07-28 00:39 -0700
SubjectRe: reading text files with indentation
Message-ID<mailman.12381.1406533604.18130.python-list@python.org>

[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

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web