Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Noob issue with populating dictionary from file. Then updating dict and writing back to file
    primarykey
    data
    text
    <p>The code below is supposed to lookup first column (key) from a file <code>Dict_file</code> and replace the first column of another file <code>fr</code>, with the value of the key found from <code>dict_file</code>. But it keeps the <code>dict_file</code> as an updated dictionary for future lookups.</p> <p>Every time the code is run, it initializes a dictionary from that dict_file file. If it finds a new email address from another file, it adds it to the bottom of the dict_file.</p> <p>It should work fine according to my understanding because if it doesn't find an @ symbol it assigns looking_for the value of "Dummy@dummy.com".. Dummy@dummy.com should be appended to the bottom of dict_file.</p> <p>But for some reason, I keep getting new lines and blank lines appended along with other new emails at the end of the dict_file. I can't be writing blanks and newlines to the end of the dict_file.</p> <p>Why is this happening? Whats wrong in the code below, my brain is about to explode! Any help will be greatly appreciated!</p> <pre><code>#!/usr/bin/python import sys d = {} line_list=[] alist=[] f = open(sys.argv[3], 'r') # Map file for line in f: alist = line.split() key = alist[0] value = alist[1] d[str(key)] = str(value) alist=[] f.close() fr = open(sys.argv[1], 'r') # source file fw = open(sys.argv[2]+"/masked_"+sys.argv[1], 'w') # target file for line in fr: columns = line.split("|") looking_for = columns[0] # this is what we need to search if looking_for in d: # by default, iterating over a dictionary will return keys if not looking_for.find("@"): looking_for == "Dummy@dummy.com" new_line = d[looking_for]+'|'+'|'.join(columns[1:]) line_list.append(new_line) else: new_line = d[looking_for]+'|'+'|'.join(columns[1:]) line_list.append(new_line) else: new_idx = str(len(d)+1) d[looking_for] = new_idx kv = open(sys.argv[3], 'a') kv.write("\n"+looking_for+" "+new_idx) kv.close() new_line = d[looking_for]+'|'+'|'.join(columns[1:]) line_list.append(new_line) fw.writelines(line_list) </code></pre> <p>Here is the dict_file:</p> <pre><code>WHATEmail@SIMPLE.COM 223 SamHugan@CR.COM 224 SAMASHER@CATSTATIN.COM 225 FAKEEMAIL@SLOW.com 226 SUPERMANN@MYMY.COM 227 </code></pre> <p>Here is the fr file that gets the first column turned into the id from the dict_file lookup:</p> <pre><code>WHATEmail@SIMPLE.COM|12|1|GDSP FAKEEMAIL@SLOW.com|13|7|GDFP MICKY@FAT.COM|12|1|GDOP SUPERMANN@MYMY.COM|132|1|GUIP MONITOR|132|1|GUIP |132|1|GUIP 00 |12|34|GUILIGAN </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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