Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

can I please get help with this Python program?


prox's Avatar
Member
0 0

I want to learn Python so I started writing my first program which is a phone book directory. It has the options to add a name and phone number, remove numbers, and search for them.

Basically, my program stores the numbers to a list in a file. I cannot figure out how to remove a particular line in the file but keep the rest of the file intact. Can someone please help me with this?

http://bpaste.net/show/zusmRFYaBsAF09GUcKce/


stranac's Avatar
Member
0 0

I already answered your question on python-forum.

Of all the things mentioned, I believe using functions would do most good for your code.


prox's Avatar
Member
0 0

I tried the…

for line in f: if line.strip() != coname.strip(): f1.write(line)

but its not working. Ive pretty much redone it and got it all broke down into functions but its still driving me crazy that I cant get this to work.


darkmistery's Avatar
Member
0 0

What kind of input are you expecting ?

If the codilist.txt file looks like : Company Name - phone number you should you regular expressions to match the name.


stranac's Avatar
Member
0 0

Ok, post your updated code, and I'll take another look.

darkmistry wrote: If the codilist.txt file looks like : Company Name - phone number you should you regular expressions to match the name.

No, using regex for something this simple would be silly.


darkmistery's Avatar
Member
0 0

Well i suppose instead of if line.strip() == coname.strip() he could use if coname in line but using a regular expression he would learn more i guess …


stranac's Avatar
Member
0 0

Oh, darkmistry is right.

markupif line.strip() == coname.strip(): wil not find the line, since the line will also contain the number.

His suggested markupif coname in line: could return some false matches, but something like this should work(assuming no spaces in phone number):

if name == coname:

My other points still stand.


prox's Avatar
Member
0 0

I played with it today and I got this working…

        coname = raw_input('What company do you want to remove? ') # company name
        f = open('codilist.txt')
        tmpfile = open('codilist.tmp', 'w')
        for line in f:
            if coname in line:
                print coname + ' has been removed.'
            else:
               tmpfile.write(line)
        f.close()
        tmpfile.close()
        os.rename('codilist.tmp', 'codilist.txt')
        continue```

stranac's Avatar
Member
0 0

Glad you got it to work.

Now try this:

2) Add a company named 'eve'
3) Remove 'eve'
4) Search for 'whatever'```
See where I was going with the false positives thing?

prox's Avatar
Member
0 0

so what does line.rsplit(' ', 1) do exactly?


stealth-'s Avatar
Ninja Extreme
0 0

prox wrote: so what does line.rsplit(' ', 1) do exactly?

Help on built-in function rsplit:

rsplit(…) S.rsplit([sep [,maxsplit]]) -> list of strings

Return a list of the words in the string S, using sep as the
delimiter string, starting at the end of the string and working
to the front.  If maxsplit is given, at most maxsplit splits are
done. If sep is not specified or is None, any whitespace string
is a separator.
>>> name, number = text.rsplit(' ', 1)
>>> name
'test'
>>> number
'123'
>>>

Try things before asking, prox.