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


Groups > comp.lang.python > #75303

Re: reading text files with indentation

Date 2014-07-28 00:39 -0700
From Gary Herron <gary.herron@islandtraining.com>
Subject Re: reading text files with indentation
References <53D5ECD0.8010702@enabled.com>
Newsgroups comp.lang.python
Message-ID <mailman.12381.1406533604.18130.python-list@python.org> (permalink)

Show all headers | 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