Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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. </p> <p>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:</p> <ol> <li>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). </li> <li>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)</li> <li>You don't need parenthesis for conditions</li> <li>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.</li> <li>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.</li> </ol> <p>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. </p> <p>Cleaning up like I mentioned, and re-building the logic, the script looks something like this:</p> <pre><code># 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) &gt;= 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() </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload