0

I am writing a simple program that offers the user an account. That includes that the user can change his or her password over and over. And if he or she includes info as their telephone number, there must be options that enable them to change their own data.

The program will open .txt files and read the info.

For example, this is a small part of the info that the .txt includes:

123$333$Harold$321$Far far away3
124$444$George$654$Far far away4   #I logged in with this account
125$555$Louis$987$Far far away5

Now suppose that George (the one who logged in) wants to change his adress. I mean it is easy to modify the info once it is on the list, split by the "$". This is how the modified list will look like:

[124, 444, 'George', 654, '22 Acacia Avenue']

But the question is how can I modify the .txt so that the contents will look like this?

123$333$Harold$321$Far far away3
124$444$George$654$22 Acacia Avenue   
125$555$Louis$987$Far far away5

How can I achieve this?

3

3 Answers 3

2

As others have suggested in the comments to your question, you might want to use solutions that are not file based - take a look at the good links provided.

If your homework requires you to use a text file (or you want to work with files for sake of learning that), you can do something like this:

inf.txt:

123$333$Harold$321$Far far away3
124$444$George$654$Far far away4
125$555$Louis$987$Far far away5

mkt.py:

# Read the file
with open('inf.txt', 'r') as f:
  lines = f.readlines()

# Split the lines by '$'
lines_split = [line.split('$') for line in lines]

# Change address of the second login
line_to_change = lines_split[1]
# Change just last column
line_to_change[-1] = '22 Acacia Avenue\n'

# Combine back
lines_out = ['$'.join(line) for line in lines_split]

# Write the file
with open('outf.txt', 'w') as f:
  f.writelines(lines_out)

And executing it:

$ python mkt.py 
$ cat outf.txt 
123$333$Harold$321$Far far away3
124$444$George$654$22 Acacia Avenue
125$555$Louis$987$Far far away5
$ 
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think its great to give them the answer straight up, but Im glad you at least broke this down with comments as opposed to pasting a block of code.
@jdi Thanks - yes, tried to be helpful in educational sense, hope it worked :)
Thanks a lot @icyrock.com Now I have my modificated file, with the new info!
0

You could read the info into a list of tuples. Then replace the tuple you want to change with what you want it to be. When you have made all the changes you want write the list back into the txt file.

Comments

0

Unless you use a text file whose fields are fixed in length, you will have to rewrite the entire text file every time you make a change (because you will be changing the start position of every field after the one you edit). As long as the file is small, you won't particularly notice this, but if the file has more than a few dozen lines this will begin to become a significant performance issue.

The commenters who suggest you use a database are largely right, although that introduces a new set of decisions about what kind of database to use, etc., which are likely the kinds of questions that this homework is designed to generate in a later section. :-)

Rewriting the file every time is trivial, however wasteful (pseudocode-ish):

new_data = [124, 444, 'George', 654, '22 Acacia Avenue']

f = open(path, mode) # Open with some mode that makes sense for how you write it
                     # or clear the file before you write your new list
lines = []

# Now we can iterate over every line in the file
for line in f:
  # Check to see if this line starts with the same data before the '$' as the
  # first field of the new_data list
  if line.split("$")[0] == new_data[0]:
    # if it does, put in new_data instead of the line we just read
    lines.append("$".join(new_data))
  else:
    # Not the line we're looking for, write it back into the file so it stays the same
    lines.append(line)
f.writelines(lines)

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.