Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8909083
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T03:20:48+00:00 2026-06-15T03:20:48+00:00

The code below is supposed to lookup first column (key) from a file Dict_file

  • 0

The code below is supposed to lookup first column (key) from a file Dict_file and replace the first column of another file fr, with the value of the key found from dict_file. But it keeps the dict_file as an updated dictionary for future lookups.

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.

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.

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.

Why is this happening? Whats wrong in the code below, my brain is about to explode! Any help will be greatly appreciated!

#!/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)

Here is the dict_file:

WHATEmail@SIMPLE.COM    223
SamHugan@CR.COM 224
SAMASHER@CATSTATIN.COM  225
FAKEEMAIL@SLOW.com  226
SUPERMANN@MYMY.COM 227

Here is the fr file that gets the first column turned into the id from the dict_file lookup:

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T03:20:49+00:00Added an answer on June 15, 2026 at 3:20 am

    Firstly, you need to ignore blanks in your initial dictionary read, otherwise you will get an index out of range error when you run this script again. Do the same when you read via the fr object to avoid entering nulls. Wrap your email check condition further out for greater scope. Do a simple check for the “@” using the find method. And you’re good to go.

    Try the below. This should work:

    #!/usr/bin/python
    
    import sys
    
    d = {}
    line_list=[]
    alist=[]
    f = open(sys.argv[3], 'r') # Persisted Dictionary File
    
    for line in f:
        line = line.strip()
        if line =="":
            continue
        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 Directory Location
    
    for line in fr:
        line = line.strip()
        if line == "":
            continue
        columns = line.strip().split('|')
        if columns[0].find("@") > 1:
            looking_for = columns[0] # this is what we need to search
        else:
            looking_for = "Dummy@dummy.com"
        if looking_for in d:
            # by default, iterating over a dictionary will return keys
                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(looking_for+" "+new_idx+'\n')
            kv.close()
            new_line = d[looking_for]+'|'+'|'.join(columns[1:])
            line_list.append(new_line)
    fw.writelines(line_list)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The below code is supposed to print: PHP XPath Example, but it doesn't. I'm
The code below is supposed to grab the ?cs=bla&cat=bla values from the link attribute
I have the code below that is supposed to draw lines from the top
My code below is supposed to display each question ($_POST[questionText]). For each question, it
Every time I run the code below it is supposed to come up with
I suppose button should has Close title in the code below, but it has
Suppose I have an XSD file having below lines of code; <xsd:simpleType name=test> <xsd:restriction
Ok another question in VHDL. Below is my code. Suppose that I want my
Code below is used to save PostgreSql database backup from browser in Apache Mono
The code below is supposed to initiate CSS based on if($row[datesubmitted] > $livetime){ .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.