Thread: Please help me.

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Posts
    1
    Rep Power
    0

    Exclamation Please help me.


    I need to make my program load email addresses and names from a text document when the .txt file includes other useless text.

    the sorce code is:


    import string

    true = 1
    false = 0

    def print_numbers(numbers):
    print "E-mail Addresses: "
    for x in numbers.keys():
    print "Name: ",x," \tAddress: ",numbers[x]
    print

    def add_number(numbers,name,number):
    numbers[name] = number

    def lookup_number(numbers,name):
    if numbers.has_key(name):
    return "The address is: "+numbers[name]
    else:
    return name+" was not found"

    def remove_number(numbers,name):
    if numbers.has_key(name):
    del numbers[name]
    else:
    print name," was not found"


    def load_numbers(numbers,filename):
    in_file = open(filename,"r")
    while true:
    in_line = in_file.readline()
    if in_line == "":
    break
    in_line = in_line[:-1]
    [name,number] = string.split(in_line,",")
    numbers[name] = number
    in_file.close()

    def save_numbers(numbers,filename):
    out_file = open(filename,"w")
    for x in numbers.keys():
    out_file.write(x+","+numbers[x]+"\n")
    out_file.close()


    def print_menu():
    print '1. Display E-mail Addresses'
    print '2. Add an address'
    print '3. Remove an address'
    print '4. Lookup an Address'
    print '5. Load Addresses'
    print '6. Save Addresses'
    print '7. Quit'
    print

    phone_list = {}
    menu_choice = 0
    print_menu()
    while menu_choice != 7:
    menu_choice = input("Choose an option (1-7):")
    if menu_choice == 1:
    print_numbers(phone_list)
    elif menu_choice == 2:
    print "Add Name and Address"
    name = raw_input("Name:")
    phone = raw_input("Address:")
    add_number(phone_list,name,phone)
    elif menu_choice == 3:
    print "Remove Address"
    name = raw_input("Name: ")
    remove_number(phone_list,name)
    elif menu_choice == 4:
    print "Lookup Address"
    name = raw_input("Name: ")
    print lookup_number(phone_list,name)
    elif menu_choice == 5:
    filename = raw_input("Filename to load: ")
    load_numbers(phone_list,filename+".txt")
    elif menu_choice == 6:
    filename = raw_input("Filename to save: ")
    save_numbers(phone_list,filename+".txt")
    elif menu_choice == 7:
    pass
    else:
    print_menu()
    print "Goodbye"
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Please read the stickies and try a new post!

IMN logo majestic logo threadwatch logo seochat tools logo