Skip to content
Sign upLog in
← Back to Community

Changing a string from a specific line in text file

Profile icon
ZestyLad

Can anyone tell me how to change the value of loan of a member here in my program? Like let's say I want to change Anne's loan value from 750 to 1000, how can I change only hers and keep the rest? i tried doing the readlines() method but I still cant get to change it, tried researching about it on stackoverflow too but still cant get something out of it. Any help would be appreciated!

image

Answered by c4syner [earned 5 cycles]
View Answer
Voters
Profile icon
ZestyLad
Comments
hotnewtop
Profile icon
c4syner
with open('abc.txt','w') as f: f.write("Members\t\tLoans"+"\nMark\t\t500"+"\nSteve\t\t1000"+'\nAnne\t750'+'\nAlpha\t520') a=input("Enter name: ") b=input("Enter new loan: ") index = 0 with open('abc.txt','rt') as f: lines = f.readlines() #readlines is crucial here for i in range(len(lines)):#Find index of line where user is if a in lines[i]: index = i #now have index replace value mainStr = lines[index] originalStr = mainStr #preserve original value curNum = int(''.join(filter(str.isdigit, mainStr))) #find current loan mainStr = mainStr.replace(str(curNum), str(b)) #change value of loan for i in range(len(lines)): #replace value of original loan if originalStr in lines[i]: lines[i] = mainStr lines = "".join(lines) #make it into a big string like before print(lines) with open('abc.txt','w') as f: f.write(lines)

This should work pretty good, supports the same file export that you had in your original code and can support more people being added.

please upvote if this helped

Profile icon
ZestyLad

@c4syner
Its giving me an error with line 15 cause of integer conversion error.

Profile icon
c4syner

@ZestyLad
just make sure you're putting the literal name of the individual. Instead of putting mark put Mark for example.

Profile icon
ZestyLad

@c4syner
Oh yeah, how dumb of me, its working now, thanks!

Profile icon
Codemonkey51

What you can do is open the file in read mode then set a variable named contents = {filevar}.read() then make any changes to contents next do {filevar}.close() open the file in write mode then {filevar}.write(contents) {filevar}.close()

Profile icon
Codemonkey51

{filevar} as in the variables you opened the file as

Profile icon
ZestyLad

@Codemonkey51
Ok. Thanks, I'll try it.

Profile icon
Codemonkey51

Your welcome sorry if it was a bit hard to understand

@ZestyLad

Profile icon
ZestyLad

@Codemonkey51
No worries! I get the idea anyway.

Profile icon
Codemonkey51
Profile icon
hendersonross

Hello Digimentor, thanks for the info. When I needed money quickly in the past, I would usually go to the bank, but because that process takes a while, I chose to use by Trice loans service instead. You may obtain a loan with this highly practical service in just a few minutes. The ability to apply for a loan online is this service's key benefit. I hope my suggestions will be helpful to you!

Profile icon
Digimentor

Dude, I'm not sure what you want to do is legal. As far as I know, the only way to change credit is to start the process of paying it off. Each type of loan has its nuances of repaymen, for example, you can extend the repayment period under certain conditions and vice versa. However, I may not know something. You must go to a mortgage specialist who can draw up the documents competently. So if you need help, then you can try Mortgage Broker Taunton https://bristolmoneyman.com/mortgage-broker-taunton/ . I think it is correct to contact experts specializing in this field because ordinary users can give the wrong advice.