2

I've a file entitled 'users.txt' with the following structure; username:info_about_the_user.

Something like this:

users.txt:

mark:stuffabouthim
anthony:stuffabouthim
peter:stuffabouthim
peterpeter:stuffabouthim
peterpeterpeter:stuffabouthim
peterpeterpeterpeter:stuffabouthim

The following part of the script needs to change a line (change info about an user) but I'm having problems when the string is duplicated (peter - peterpeter) and I dont know how to fix it.

def test():
    fn = 'users.txt'
    f = open(fn)
    output = []
    changeuser = 'peterpeter'
    userinfo = 'HeIsTall'
    for line in f:
        if not changeuser+":" in line:
            output.append(line)
    f.close()
    f = open(fn, 'w')
    f.writelines(output)
    f.close()
    f = open("users.txt", "a")
    f.write(changeuser + ":" + userinfo+"\n")
    f = open("users.txt", "a")

test()

This is the input I have:

Input: users.txt:

mark:stuffabouthim
anthony:stuffabouthim
peter:stuffabouthim
peterpeter:HesAwesome
peterpeterpeter:stuffabouthim
peterpeterpeterpeter:stuffabouthim

I want to change info about peterpeter and have the following output:

Output I want to have: users.txt:

mark:stuffabouthim
anthony:stuff about him
peter:stuffabouthim
peterpeter:HeIsTall
peterpeterpeter:stuffabouthim
peterpeterpeterpeter:stuffabouthim

But this is the input I'm having. All the lines behind peterpeter are getting deleted among other things.

mark:stuffabouthim
anthony:stuffabouthim
peter:stuffabouthim
peterpeter:HeIsTall

Can anyone give me a help with the code below to have the desired output? Thanks.

2
  • Do you really need python for this? You might be better off with awk + sed linux commands. Commented Jul 6, 2012 at 9:57
  • and if he would want to do it in assembler he would have a reason to do so - btw. it almost certainly looks like homework to me! ;-) Commented Jul 7, 2012 at 22:36

3 Answers 3

3

You can have it the easy way with the fileinput module:

import fileinput

def test():
    fn = 'users.txt'

    changeuser = 'peterpeter'
    newinfo = 'HeIsTall'

    for line in fileinput.input(fn, inplace=1):
        user, oldinfo = line.split(':')
        print '%s:%s' % (user, newinfo if user == changeuser else oldinfo.replace('\n', ''))

if __name__ == "__main__":
    test()
Sign up to request clarification or add additional context in comments.

Comments

2

try this:

def test():
    fn = 'users.txt.txt'
    f = open(fn)
    output = []
    changeuser = 'peterpeter'
    userinfo = 'HeIsTall'
    for line in f:
        if line.strip().split(':')[0]!=changeuser:
            output.append(line)
        else:
            output.append(changeuser + ":" + userinfo+"\n")

    f.close()
    f = open(fn, 'w')
    f.writelines(output)
    f.close()

test()

output:

mark:stuffabouthim
anthony:stuffabouthim
peter:stuffabouthim
peterpeter:HeIsTall
peterpeterpeter:stuffabouthim
peterpeterpeterpeter:stuffabouthim

1 Comment

It's mostly ok, but that appends it on the last line. like this: peterpeterpeterpeter:stuffabouthimpeterpeter:HeIsTall Do you know how to put them in different lines?
2

You got a logical error in the if-clause, which DELETES all peters*, the only peter remaining is the one you append to the file.

for line in f:
        if not changeuser+":" in line:  #THAT MEANS ALL PETERS ARE IGNORED!
            output.append(line)

It's generaly easier to understand positive clauses then a negation:

for line in f:
   if changeuser+":" in line:
      output.append('%s:%s\n' %(changeuser,userinfo))
   else:
      output.append(line)

Good code is easy to read. Try to code like you would try to write a report! That leads automatically to spliting your code into smaller pieces like functions. e.g.:

lines = read_all_lines_from_file(filename)
change_user_info(lines, user, userinfo)
save_lines_to_file(lines, filename)

Your code gets split into smaller pieces and if an error occurs you can pin it down to a few lines of code instead of having to work over several pages. ;-)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.