Answer the question
In order to leave comments, you need to log in
How to replace part of text in a string?
There is a file with lines.
It is necessary, upon request, to find a line in the file containing a certain word and then replace it with a new one.
The contents of the bot_users.ini file:
76561198350488605 = 20190926211922 20191006000222 0 71791 0 0 KraimLoc
76561198006283363 = 20190926211949 20200726110443 0 71956 0 417 sHERK1
76561198072107799 = 20190926212515 20200727145756 0 72266 0 527 Накурин
76561198868608679 = 20190926215315 20200327061846 0 65996 0 5435 Tolik Odessa
76561198990369306 = 20190926215454 20191207161519 0 71991 0 3591 GoodMan
76561198161930938 = 20190926220107 20200617114901 0 44004 0 Antoxa_102
76561198985130944 = 20190926220336 20191215185342 0 76516 0 4184 МАРВИН
76561198042056253 = 20190926220911 20200722134832 0 85871 0 0 AnaRchisT
76561198880335186 = 20190926221129 20190926221129 0 71791 0 4258 默秋
myfile = 'bot_users.ini'
word = '76561198985130944'
cash = 500
f = open(myfile, 'r+', encoding="utf-8")#открыл файл
for line in f:#читаю строки
if word in line:#ищу слово word = 76561198985130944 в строках
new_list = line.split(' ')#создаю список из строки
print(new_list)#для проверки вывожу на экран
#присваиваю значения
steamid = new_list[0]
name = new_list[8]
bank = new_list[5]
newbank = int(bank) + cash#создаю (задаю) новое значение newbank
print('New Bank: {}'.format(newbank))#проверка значения newbank
print('>>>>>>>>Found: {}\nSteamid: {}\nName: {}\nBank: {}'.format(word, steamid, name, bank))
Answer the question
In order to leave comments, you need to log in
I have no words. To begin with, all this code is complete rubbish, everything can be written much clearer and more sensibly, but this is just nonsense.
Further, I would recommend using a local database to store all sorts of rubbish like this.
In theory it should work, I haven't tested it.
myfile = 'bot_users.ini'
word = '76561198985130944'
cash = 500
f = open(myfile, 'r+', encoding="utf-8")#открыл файл
lines = f.read().strip().split("\n")
for line in lines:#читаю строки
if word in line:#ищу слово word = 76561198985130944 в строках
lines.remove(line)
new_list = line.split(' ')#создаю список из строки
print(new_list)#для проверки вывожу на экран
#присваиваю значения
steamid = new_list[0]
name = new_list[8]
bank = new_list[5]
newbank = int(bank) + cash#создаю (задаю) новое значение newbank
lines.append(" ".join(new_list).replace(bank, str(newbank)))
print('New Bank: {}'.format(newbank))#проверка значения newbank
print('>>>>>>>>Found: {}\nSteamid: {}\nName: {}\nBank: {}'.format(word, steamid, name, bank))
f.truncate(0)
f.write("\n".join(lines))
f.close()
Ternick Thank you very much for your help!
I added the code so that the text replacement would occur in the same line!
myfile = 'bot_users.ini'
word = '76561198868608679'
cash = 500
f = open(myfile, 'r', encoding="utf-8")#открыл файл
lines = f.read().strip().split("\n")
for i, line in enumerate(lines):#ищу № строки
#for line in lines:#читаю строки# - заменил на строку, указанную выше (что бы определить № строки)
if word in line:#ищу слово word = 76561198161930938 в строках
lines.remove(line)
new_list = line.split(' ')
steamid = new_list[0]
name = str(new_list[8])
bank = new_list[5]
newbank = int(bank) + cash#создаю (задаю) новое значение newbank
new_list[5] = str(newbank)
lines.insert(i, " ".join(new_list))#вставляю в туже самую строку данные
print('New Bank: {}'.format(newbank))#проверка значения newbank
print('>>>Found!\nLine {}: {}\nSteamid: {}\nName {}\nBank: {}'.format(i, word, steamid, name, bank))
f.close()
f = open(myfile, 'w', encoding="utf-8")
f.write("\n".join(lines))
f.close()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question