-4

Hello I'm facing a problem and I don't how to fix it. All I know is that when I add an else statement to my if statement the python execution always goes to the else statement even there is there a true statement in if and can enter the if statement.

Here is the script, without the else statement:

import re
f = open('C:\Users\Ziad\Desktop\Combination\MikrofullCombMaj.txt', 'r')
d = open('C:\Users\Ziad\Desktop\Combination\WhatsappResult.txt', 'r')
w = open('C:\Users\Ziad\Desktop\Combination\combination.txt','w')
s=""
av =0
b=""
filtred=[]
Mlines=f.readlines()
Wlines=d.readlines()

for line in Wlines:
    Wspl=line.split()
    for line2 in Mlines:
        Mspl=line2.replace('\n','').split("\t")
        if ((Mspl[0]).lower()==(Wspl[0])):
            Wspl.append(Mspl[1])
            if(len(Mspl)>=3):
                Wspl.append(Mspl[2])
                s="\t".join(Wspl)+"\n"
            if s not in filtred:
                filtred.append(s)
                break
for x in filtred:
    w.write(x)
f.close()
d.close()
w.close()

with the else statement and I want else for the if ((Mspl[0]).lower()==(Wspl[0])):

import re
f = open('C:\Users\Ziad\Desktop\Combination\MikrofullCombMaj.txt', 'r')
d = open('C:\Users\Ziad\Desktop\Combination\WhatsappResult.txt', 'r')
w = open('C:\Users\Ziad\Desktop\Combination\combination.txt','w')
s=""
av =0
b=""
filtred=[]
Mlines=f.readlines()
Wlines=d.readlines()
for line in Wlines:
    Wspl=line.split()
    for line2 in Mlines:
        Mspl=line2.replace('\n','').split("\t")
        if ((Mspl[0]).lower()==(Wspl[0])):
            Wspl.append(Mspl[1])
            if(len(Mspl)>=3):
                Wspl.append(Mspl[2])
                s="\t".join(Wspl)+"\n"
            if s not in filtred:
                filtred.append(s)
                break
        else:
            b="\t".join(Wspl)+"\n"
            if b not in filtred:
                filtred.append(b)
                break
for x in filtred:
    w.write(x)
f.close()
d.close()
w.close()
Santosh Kumar
  • 26,475
  • 20
  • 67
  • 118
Z. Kiwan
  • 123
  • 1
  • 14
  • 2
    Step through the code with the debugger. – Jack Aidley Aug 22 '13 at 10:06
  • 3
    Make sure you're not mixing tabs and spaces. Doing so can cause your indentation to look right, but not really be so. – martineau Aug 22 '13 at 10:09
  • i checked out all the indentation they are right i used tab for all – Z. Kiwan Aug 22 '13 at 10:15
  • there is no debuggers for python codes ? – Z. Kiwan Aug 22 '13 at 10:18
  • if nothing else, import pdb See http://stackoverflow.com/questions/4228637/getting-started-with-the-python-debugger-pdb Or just print (Mspl[0]).lower() and (Wspl[0]) in the else part to check – doctorlove Aug 22 '13 at 10:48
  • 1
    How did you check the indentation? You might be better off changing tabs to spaces, just to be sure – doctorlove Aug 22 '13 at 10:50
  • 2
    PEP008: "Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. When invoking the Python 2 command line interpreter with the -t option, it issues warnings about code that illegally mixes tabs and spaces. When using -tt these warnings become errors. " – cdarke Aug 22 '13 at 10:55

1 Answers1

3

first of all, you're not using "re" at all in your code besides importing it (maybe in some later part?) so the title is a bit misleading.

secondly, you are doing a lot of work for what is basically a filtering operation on two files. Remember, simple is better than complex, so for starters, you want to clean your code a bit:

  1. you should use a little more indicative names than 'd' or 'w'. This goes for 'Wsplt', 's' and 'av' as well. Those names don't mean anything and are hard to understand (why is the d.readlines named Wlines when ther's another file named 'w'? It's really confusing).
  2. If you choose to use single letters, it should still make sense (if you iterate over a list named 'results' it makes sense to use 'r'. 'line1' and 'line2' however, are not recommanded for anything)
  3. You don't need parenthesis for conditions
  4. You want to use as little variables as you can as to not get confused. There's too much different variables in your code, it's easy to get lost. You don't even use some of them.
  5. you want to use strip rather than replace, and you want the whole 'cleaning' process to come first and then just have a code the deals with the filtering logic on the two lists. If you split each line according to some logic, and you don't use the original line anywhere in the iteration, then you can do the whole thing in the beggining.

Now, I'm really confused what you're trying to achieve here, and while I don't understand why your doing it that way, I can say that looking at your logic you are repeating yourself a lot. The action of checking against the filtered list should only happend once, and since it happens regardless of whether the 'if' checks out or not, I see absolutely no reason to use an 'else' clause at all.

Cleaning up like I mentioned, and re-building the logic, the script looks something like this:

# PART I - read and analyze the lines
Wappresults = open('C:\Users\Ziad\Desktop\Combination\WhatsappResult.txt', 'r')
Mikrofull = open('C:\Users\Ziad\Desktop\Combination\MikrofullCombMaj.txt', 'r')

Wapp = map(lambda x: x.strip().split(), Wappresults.readlines())
Mikro = map(lambda x: x.strip().split('\t'), Mikrofull.readlines())

Wappresults.close()
Mikrofull.close()

# PART II - filter using some logic
filtred = []

for w in Wapp:
    res = w[:] # So as to copy the list instead of point to it
    for m in Mikro:
        if m[0].lower() == w[0]:
            res.append(m[1])
            if len(m) >= 3 : 
                res.append(m[2])

        string = '\t'.join(res)+'\n' # this happens regardles of whether the 'if' statement changed 'res' or not
        if string not in filtred:
            filtred.append(string)


# PART III - write the filtered results into a file
combination = open('C:\Users\Ziad\Desktop\Combination\combination.txt','w')
for comb in filtred:
    combination.write(comb)
combination.close()

I can't promise it will work (because again, like I said, I don't know what you're trying to achive) but this should be a lot easier to work with.

yuvi
  • 18,155
  • 8
  • 56
  • 93
  • 2
    Note `l` is not recommended as a variable name because it can look like `1` depending on the font being used. – Burhan Khalid Aug 22 '13 at 13:00
  • I agree that it isn't a good name ('lines' neither), though I never had the problem that you mentioned. Nevertheless, I changed it to 'r' – yuvi Aug 22 '13 at 13:06
  • @yuvi: capital `L` is free to use as replacement. – mike3996 Aug 22 '13 at 13:07
  • @progo using a capital letter for iteration? OH NO, that would be awful =P – yuvi Aug 22 '13 at 13:42
  • Hello i understand what are those Variables because they are abbreviations of the words i mean :), about python i'm new to it thats why i'm facing such problems :/ anyway thanks i found a solution myself :D ty again – Z. Kiwan Sep 30 '13 at 07:03