[Tutor] Working with lines from file and printing to another keeping sequential order
"Shantanoo Mahajan (शंत
"Shantanoo Mahajan (शंत
Sat Apr 25 20:41:55 CEST 2009
On 25-Apr-09, at 11:41 PM, Dan Liang wrote:
> Hi Bob and tutors,
>
> Thanks Bob for your response! currently I have the current code, but
> it does not work:
>
> ListLines= []
> for line in open('test.txt'):
> line = line.rstrip()
> ListLines.append(line)
>
> for i in range(len(ListLines)):
>
> if ListLines[i].endswith("yes") and ListLines[i
> +1].endswith("no") and ListLines[i+1].endswith("no"):
> print ListLines[i], ListLines[i+1], ListLines[i+2]
> elif ListLines[i].endswith("yes") and ListLines[i
> +1].endswith("no"):
> print ListLines[i], ListLines[i+1]
> elif ListLines[i].endswith("yes"):
> print ListLines[i]
> elif ListLines[i].endswith("no"):
> continue
> else:
> break
>
> I get the following error:
> Traceback (most recent call last):
> File "test.py", line 18, in <module>
> if ListLines[i].endswith("yes") and ListLines[i
> +1].endswith("no") and ListLines[i+1].endswith("no"):
> IndexError: list index out of range
You need to put check for
i <= len(ListLines)-2
or you can have a look @ try and except (exception handling in python)
> Lines in the file look like following:
>
> word1 word2 word3 word4 yes
> word1 word2 word3 word4 no
> word1 word2 word3 word4 no
> word1 word2 word3 word4 no
> word1 word2 word3 word4 yes
> word1 word2 word3 word4 no
> word1 word2 word3 word4 yes
> word1 word2 word3 word4 no
> word1 word2 word3 word4 yes
> word1 word2 word3 word4 yes
> word1 word2 word3 word4 no
> word1 word2 word3 word4 no
> word1 word2 word3 word4 no
> word1 word2 word3 word4 yes
> word1 word2 word3 word4 no
> word1 word2 word3 word4 yes
> word1 word2 word3 word4 yes
> word1 word2 word3 word4 no
> word1 word2 word3 word4 no
> word1 word2 word3 word4 no
> word1 word2 word3 word4 no
> word1 word2 word3 word4 yes
>
> > What do we do at steps 4ff if the line does not end in "yes" or
> "no"?
>
> I forgot to mention that I know for sure that the file has ONLY
> lines that end in either "yes" or "no".
>
> Any suggestions are appreciated. Also, I feel that my code is not
> the best way of solving the problem even if the problem of list
> indices is solved. Is my guess right?
>
Another suggestion. I would not read all data from file into array
(memory). If your file is large, you may hit out of memory error.
regards,
shantanoo
> Thank you!
>
> -dan
>
> On Sat, Apr 25, 2009 at 10:32 AM, bob gailer <bgailer at gmail.com>
> wrote:
> Dan Liang wrote:
> Dear Tutors,
>
>
> I have a file from which I want to extract lines that end in certain
> strings and print to a second file. More specifically, I want to:
>
> 1) iterate over each line in the file, and if it ends in "yes",
> print it.
> 2) move to the line following the one described in #1 above, and if
> it ends in, "no" print it.
> 3) move to third line, and if it ends in "no", print it.
> 4) move to fourth line, and if it ends in "no" discard it, but if it
> ends in "yes" repeat 1, 2, and 3 above.
> 5) move to fifth line, and if it ends in "no" discard it, but if it
> ends in "yes" repeat 1, 2, 3, and 4 above, and so on.
>
> The goal is to get a ratio of 1 to 2 "yes" to "no" lines from a file
> in such a way that keeps the order of the lines in output. An
> abstraction away from this so that any ratio of "yes" to "no" lines
> could be printed while keeping the order of the original lines would
> be great.
>
> Please show us what code you have written, and in what way it fails
> to meet your expectations.
>
> Your specification is IMHO a nice piece of pseudocode which could
> translate to Python fairly easily!
>
> What do we do at steps 4ff if the line does not end in "yes" or "no"?
>
> If you have not written any code make a stab at it. You could start
> by asking "how in Python does one":
> open a file?
> iterate (loop)?
> get the next line from a file?
> test for equality?
> examine the end of a string?
>
>
> --
> Bob Gailer
> Chapel Hill NC
> 919-636-4239
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090426/17f6d36b/attachment-0001.htm>
More information about the Tutor
mailing list