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


Groups > comp.lang.python > #48851

Re: Simple I/O problem can't get solved

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'elif': 0.05; 'f.close()': 0.09; 'filename': 0.09; 'iterate': 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; 'trailing': 0.09; 'python': 0.11; 'accepts': 0.16; 'len(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; 'true:': 0.16; 'url:html)': 0.16; 'whitespace.': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'subject:problem': 0.24; 'this:': 0.26; 'subject:/': 0.26; 'code:': 0.26; 'header:X-Complaints- To:1': 0.27; 'character': 0.29; 'errors': 0.30; 'lines': 0.31; 'coded': 0.31; 'file:': 0.31; 'prints': 0.31; 'url:se': 0.31; 'allows': 0.31; 'file': 0.32; 'text': 0.33; 'screen': 0.34; 'but': 0.35; 'version': 0.36; 'false': 0.36; 'subject:Simple': 0.36; 'problems': 0.38; 'to:addr:python-list': 0.38; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'remove': 0.60; 'solve': 0.60; 'break': 0.61; 'first': 0.61; 'name': 0.63; 'combining': 0.68; 'line,': 0.68; 'reads': 0.68; 'user,': 0.69; 'subject:get': 0.81
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Simple I/O problem can't get solved
Date Fri, 21 Jun 2013 11:15:59 +0200
Organization None
References <4785de5f-a84d-4417-9709-68bf2f4076e8@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p508493cd.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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 <http://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 <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3656.1371806106.3114.python-list@python.org> (permalink)
Lines 51
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1371806106 news.xs4all.nl 15996 [2001:888:2000:d::a6]:46464
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:48851

Show key headers only | View raw


nickgan.sps@windowslive.com wrote:

> I have recently started to solve my first Python problems but I came
> across to this:
> Write a version of a palindrome recogniser that accepts a file name from
> the user, reads each line, and prints the line to the screen if it is a
> palindrome.(by http://www.ling.gu.se/~lager/python_exercises.html)
> 
> I think I coded it correctly but still I get no output. I do not have any
> errors I just do not get any output.
> 
> This is the code:

> import is_palindrome
> 
> filename = raw_input('Enter a file: ') # A text file
> f = open(filename)
> while True:
>         line = f.readline()

The line includes the trailing newline character which breaks the symmetry:

>>> "abba" == "abba"[::-1]
True
>>> "abba\n" == "abba\n"[::-1]
False

Use

word = line.strip()

to remove all leading and trailing whitespace.

>         if len(line) == 0:
>                 break
>         elif is_palindrome.is_palindrome(line): 
>                 print line,
> f.close()

Note that python allows you to iterate over the lines of a file with

for line in f:
    ...

Combining these modifications:

for line in f:
    word = line.strip()
    if is_palindrome.is_palindrome(word):
        print word

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


Thread

Simple I/O problem can't get solved nickgan.sps@windowslive.com - 2013-06-21 01:57 -0700
  Re: Simple I/O problem can't get solved Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2013-06-21 12:11 +0300
  Re: Simple I/O problem can't get solved Peter Otten <__peter__@web.de> - 2013-06-21 11:15 +0200
    Re: Simple I/O problem can't get solved nickgan.sps@windowslive.com - 2013-06-21 02:20 -0700
  Re: Simple I/O problem can't get solved Chris Angelico <rosuav@gmail.com> - 2013-06-22 19:07 +1000
  Re: Simple I/O problem can't get solved Peter Otten <__peter__@web.de> - 2013-06-22 12:11 +0200
  Re: Simple I/O problem can't get solved Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-06-22 12:58 -0400

csiph-web