Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #75302
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.fsmpi.rwth-aachen.de!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'subject:text': 0.05; 'that?': 0.05; 'intermediate': 0.07; 'iterate': 0.09; 'lines:': 0.09; 'lost.': 0.09; 'newline': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'subject:files': 0.09; 'trailing': 0.09; 'cheers': 0.12; '"r")': 0.16; 'files:': 0.16; 'line)': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'wrote:': 0.18; 'print': 0.22; 'header:User-Agent:1': 0.23; 'replace': 0.24; 'file.': 0.24; 'header:X-Complaints-To:1': 0.27; 'correct': 0.29; 'code': 0.31; 'getting': 0.31; 'lines': 0.31; '"",': 0.31; 'file:': 0.31; 'indentation': 0.31; 'file': 0.32; 'text': 0.33; 'open': 0.33; 'comment': 0.34; 'there,': 0.34; 'subject:with': 0.35; 'method': 0.36; 'list': 0.37; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'how': 0.40; 'remove': 0.60; 'read': 0.60 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: reading text files with indentation |
| Date | Mon, 28 Jul 2014 09:45:33 +0200 |
| Organization | None |
| References | <53D5ECD0.8010702@enabled.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd8439.dip0.t-ipconnect.de |
| User-Agent | KNode/4.11.5 |
| 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.12380.1406533557.18130.python-list@python.org> (permalink) |
| Lines | 42 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1406533557 news.xs4all.nl 2853 [2001:888:2000:d::a6]:35097 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:75302 |
Show key headers only | View raw
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()
> policy_lines.append(line)
> print line
>
> Cheers
for line in files:
with open(file) as lines:
for line in lines: # you can iterate over the file directly;
# no need for the intermediate list built by
# the readlines() method
line = line.partition("#")[0] # remove comment
line = line.rstrip() # remove trailing whitespace
policy_lines.append(line)
print line
If you want to keep trailing whitespace other than the newline replace
line = line.rstrip()
with
line = line.rstrip("\n\r")
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: reading text files with indentation Peter Otten <__peter__@web.de> - 2014-07-28 09:45 +0200
csiph-web