tags:

views:

133

answers:

1

Hi, I´m trying to read from a file, and then see if 2 Lines in there are equal if so then break the while loop. I cant recive the information from line2. In other words if line1==line2. In anywhere in the file.

example: line1 = [0,2,3,5,6], line2=[0,2,3,5,6]

I have used the readin code in a another file, and works like a charm. But not in here don´t know why. Please guys help me out.

while gameSet <26:


    cardList.sort()
    cardList = [card for card in cardList if card];
    for i in range(len(cardList)):
        cardList.sort();
        cardList[i]-=1;
        newHeap = len(cardList);

    cardList.append(newHeap);        
    print gameSet, ": cardList in loop: ", cardList;
    forfile = str(cardList);

    #Put output to file
    fileName = "file13.txt";
    inFile = open(fileName, "a")
    for i in range(1):
        add = (str(forfile)+"\n");
        inFile.write(str(add));
    inFile.close();

    #Read file and compare
    inFile=open(fileName)
    while True:
        line1 = inFile.readline()
        line2 = inFile.readline()
        if not line2: break
    print "line1: ",line1,"line2: ",line2
    if (line1==line2) & (line1!="") & (line2!="") :
        break 
    inFile.close;
    gameSet +=1;

This is the output:

Game starts with this:[5.0, 5.0] 1 : cardList in loop: [4.0, 4.0, 2] line1: [4.0, 4.0, 2] line2:
2 : cardList in loop: [1, 3.0, 3.0, 3] line1: line2:
3 : cardList in loop: [0, 2.0, 2.0, 2, 4] line1: [0, 2.0, 2.0, 2, 4] line2:
4 : cardList in loop: [1.0, 1.0, 1, 3, 4] line1: line2:
5 : cardList in loop: [0.0, 0.0, 0, 2, 3, 5] line1: [0.0, 0.0, 0, 2, 3, 5] line2:
6 : cardList in loop: [1, 2, 4, 3] line1: line2:
7 : cardList in loop: [0, 1, 2, 3, 4] line1: [0, 1, 2, 3, 4] line2:
8 : cardList in loop: [0, 1, 2, 3, 4] line1: line2:
9 : cardList in loop: [0, 1, 2, 3, 4] line1: [0, 1, 2, 3, 4] line2:
cardList: [0, 1, 2, 3, 4]

+1  A: 

Let's see the inner while loop:

while True:
    line1 = inFile.readline()
    line2 = inFile.readline()
    if not line2: break

It uses the readline method, which also reads in the newline character(s) depending on your platform. So this loop will terminate only at the end of the file. Depending on the number of lines in your file it will result in two empty strings (in the case of even number of total lines) or the last single line (in the case of odd number of total lines). For example:

  • Even number of lines:

    A
    B
    C
    D
    

It will result in line1 == '' and line2 == ''.

  • Odd number of lines:

    A
    B
    C
    

It will result in line1 == 'C\n' and line2 == ''.

None of them is what you want. There's no two lines there in those two variables. Your code right after this inner loop simply don't receive those lines at all.

UPDATE: Solution to find the first duplicated line in the file:

Please replace the inner while loop above with the following:

lines = set()
line = None
for line in inFile:
    if line in lines:
        break
    lines.add(line)
else:
    line = None

If line is not None after this inner loop, then it is the duplicated line. If line is None, then there are no duplicated lines in that file.

Is it what you need?

UPDATE: According to the latest "specification" the solution would be:

# Read file and compare
inFile = open(fileName)
previous_line = None
line = None
for line in inFile:
    if line == previous_line:
        break
    previous_line = line
else:
    line = None
inFile.close()
if line is not None:
    break

Does it do what you want to achieve?

fviktor
thanks for the replay, do you know how I can fix this?
Farhad
Do you need to find two equal lines at the end of the file (last two lines) or anywhere in the file? Do you need to find two consecutive lines or just two equal one anywhere in the file?
fviktor
anywhere in the file.
Farhad
But they should be directly under each other...
Farhad
It finds the dublicate line in the file, but doesn´t break the while gameSet loop. How can i achive that. Thank you for the help.
Farhad
Ok, then you haven't answered correctly to my previous questions. Please note the word "consecutive" above. Adding that code as well, that's even faster than finding them anywhere.
fviktor
sorry about that.. my english is as good as my programming skills lol
Farhad
works great, THANK YOU. Only one last thing how can output the result with the largest number first in each element? for example. [3,2,0,4] to be outputet [4,3,2,0] or even delete 0.
Farhad
I´ve tried with cardList.sort() but the output is: NONE
Farhad
Sort it in reverse order before you print it out. The sort method has an argument for this: `cardList.sort(reverse=True)`
fviktor
I tried that before and it didnt work but now it does lol, well I used it properly in wrong place. Works great now. Do you have a easy solution for removing the zeros form the cardList allso? If you have time.
Farhad
**Important:** Sorting the list is an in-place operation. But I think it is not a problem in your specific case, since you start your outer loop with sorting it anyway. But better to be aware of this.
fviktor
No, time is what I don't have. ;-) But seriously, try this: `cardList = [card for card in cardList if card]` And before you ask for this, if you need to remove all the non-positive (zero or negative) ones, then this one: `cardList = [card for card in cardList if card > 0]`
fviktor
Please try me give you a suggestion: Try to complete the Python tutorial at least before trying to develop larger scripts / applications in Python. It is also a good idea to study the language structures first, like the so called list comprehension above. You'll find all the teaching material you need at http://www.python.org - Just my 2 cents...
fviktor
It didn´t remove the zeros.. :/
Farhad
I´ve just started with python, and to be honest I love it and would like to learn more. I´ve been in python.org... and googled a lot but couldn´t resolve my problem with the "lines" :)
Farhad
I pasted cardList = [card for card in cardList if card > 0] after cardList.append(newHeap) and works now. Thank a lot for the help. You saved my day.
Farhad