Hi,
I'm writing a program - a "social networking site" - where the user is asked to input a message to another user. I can't figure out how to take user input and write it in a specific place in a file containing user data. From the file I convert all the data into a main dictionary.
My file looks something like this:
username1
password1
address1
age1
messages1:
username2
password2
address2
age2
messages2:
username3
password3
address3
age3
messages3:
username4
password4
address4
age4
messages4:
username6
password6
address6
age6
messages6:
From the file I convert all the data into a main dictionary:
Here's the leave a message function that I have atm:
Any help is much appreciated!
I'm writing a program - a "social networking site" - where the user is asked to input a message to another user. I can't figure out how to take user input and write it in a specific place in a file containing user data. From the file I convert all the data into a main dictionary.
My file looks something like this:
username1
password1
address1
age1
messages1:
username2
password2
address2
age2
messages2:
username3
password3
address3
age3
messages3:
username4
password4
address4
age4
messages4:
username6
password6
address6
age6
messages6:
From the file I convert all the data into a main dictionary:
Code:
userData = open("data.txt", "r")
temp = userData.read().split("\n")
mainDict = {}
for i in range(int(len(temp)/6)):
lis = [temp[i*6], temp[i*6+1], temp[i*6+2], temp[i*6+3], temp[i*6+4], temp[i*6+5]]
mainDict[temp[i*6]] = lis
userData.close()
Here's the leave a message function that I have atm:
Code:
def leaveMessage(userData):
saveFile = open("data.txt", "r")
temp = saveFile.read().split("\n")
mainDict = {}
for i in range(int(len(temp)/6)):
lis = [temp[i*6], temp[i*6+1], temp[i*6+2], temp[i*6+3], temp[i*6+4],\
temp[i*6+5]]
mainDict[temp[i*6]] = lis
saveFile.close()
recipient = input("Who do you wish to message? ")
print()
if recipient in mainDict:
userMessage = input("Please enter your message: ")
mainDict[recipient][5] = userMessage, "from", userData[0]
newDict = open("data.txt","w")
# Not sure how to change dictionary and have the text file change
# accordingly.
else:
print("User does not exist. Please enter a valid user.")