'How do I replace a line in a text file thats not in the current loop Python
I am new to programming so If there's an easier way to go about this, please let me know. There is a text file called Duelist.txt
that contains the following:
-------------------------------------------------
Duelist: Seto Kaiba
Server: Amazon
Username: Kaibaman
Password: Blue-Eyes White Dragon
-------------------------------------------------
Duelist: Mokuba Kaiba
Server: Amazon
Username: Kaibaboy
Password: KaibabestBro
------------------------------------------------
This changeUsername(login,duelistID,exists,server):
function runs if login
and exists
are TRUE
. If they want to make the change the username it prompts the user for new username and then the function is supposed to change the username in the Duelist.txt
two lines after the current location, but it does not...
def changeUsername(login,duelistID,exists,server):
index = 0
username = "Username: "
serverSearch = []
duel = "Duelist: " + duelistID
search = "Server: " + server
if login == True and exists ==True:
print("Would you like to change this username for " + server + "?")
print("Press \"y\" key for yes or press \"n\" key for no followed by the Enter key")
choice = input(f'{duelistID}:')
if choice == "y":
newUsername = input("Please enter new username: ")
username += newUsername
with open("Duelist.txt", 'r') as file:
content = file.readlines()
for line in content:
serverSearch.append(line.strip("\n"))
with open("Duelist.txt", 'r+') as file:
for index, line in enumerate(serverSearch):
if duel in line:
if search == serverSearch[index+1]:
serverSearch[index+2] = username
file.write(line.replace(serverSearch[index+2], username))
if choice == "n":
pass
print("Exiting Edit Server Username...")
time.sleep(1)
This is the user input in the function changeUsername(login,duelistID,exists,server)
with login
and exists
as True, duelistID
as Mokuba Kaiba, and server
as Amazon.
Would you like to change this username for Amazon?
Press "y" key for yes or press "n" key for no followed by the Enter key
Mokuba Kaiba:y
Please enter new username: Mokuba
Exiting Edit Server Username...
After the user input I get the following in the Duelist.txt
Duelist: Mokuba Kaiba---------------------------
Duelist: Seto Kaiba
Server: Amazon
Username: Kaibaman
Password: Blue-Eyes White Dragon
-------------------------------------------------
Duelist: Mokuba Kaiba
Server: Amazon
Username: Kaibaboy
Password: KaibabestBro
-------------------------------------------------
I've had a hard time trying to figure this out and make a simple solution but I'm just a third rate coder with a fourth rate code. Can anyone help make this function do what it's supposed to do?
Solution 1:[1]
I think I need to rephrase my Question but Pervez was able to make the changes necessary for what I wanted. I just added a little more adjustments to come up with the following:
def changeUsername(login,duelistID,exists,server):
index = 0
username = "Username: "
serverSearch = []
duel = "Duelist: " + duelistID
search = "Server: " + server
if login == True and exists ==True:
print("Would you like to change this username for " + server + "?")
print("Press \"y\" key for yes or press \"n\" key for no followed by the Enter key")
choice = input(f'{duelistID}:')
if choice == "y":
newUsername = input("Please enter new username: ")
username += newUsername
with open("Duelist.txt", 'r') as file:
content = file.readlines()
for line in content:
serverSearch.append(line.strip("\n"))
for index, line in enumerate(serverSearch):
if duelistID in line:
if search == serverSearch[index+1]:
serverSearch[index+2]=(serverSearch[index+2]).replace(serverSearch[index+2],username)
with open("Duelist.txt","w") as file:
file.write('\n'.join(serverSearch))
if choice == "n":
pass
print("Exiting Edit Server Username...")
In this case, the function will only replace the text in Duelist.txt
if the function can match the duelistID
and search
lines. Hopefully I explained this right.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | bobku |