473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

How to send data to specific line in text file

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:

Expand|Select|Wrap|Line Numbers
  1. userData = open("data.txt", "r")
  2. temp = userData.read().split("\n")
  3.  
  4.     mainDict = {}
  5.     for i in range(int(len(temp)/6)):
  6.  
  7.         lis = [temp[i*6], temp[i*6+1], temp[i*6+2], temp[i*6+3], temp[i*6+4], temp[i*6+5]]
  8.  
  9.         mainDict[temp[i*6]] = lis
  10.  
  11. userData.close()

Here's the leave a message function that I have atm:

Expand|Select|Wrap|Line Numbers
  1. def leaveMessage(userData):
  2.     saveFile = open("data.txt", "r")
  3.     temp = saveFile.read().split("\n")
  4.  
  5.     mainDict = {}
  6.     for i in range(int(len(temp)/6)):
  7.  
  8.         lis = [temp[i*6], temp[i*6+1], temp[i*6+2], temp[i*6+3], temp[i*6+4],\
  9.               temp[i*6+5]]
  10.  
  11.         mainDict[temp[i*6]] = lis
  12.  
  13.  
  14.     saveFile.close()
  15.  
  16.     recipient = input("Who do you wish to message? ")
  17.     print()
  18.  
  19.     if recipient in mainDict:
  20.         userMessage = input("Please enter your message: ")
  21.         mainDict[recipient][5] = userMessage, "from", userData[0]
  22.  
  23.         newDict = open("data.txt","w")
  24.         # Not sure how to change dictionary and have the text file change
  25.         # accordingly.
  26.  
  27.     else:
  28.         print("User does not exist. Please enter a valid user.")
Any help is much appreciated!
Mar 20 '11 #1
0 1389

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: adrian GREEMAN | last post by:
When I try to import a text file with new data for an existing table I get the error "1148 - the used command is not allowed with this MySQL version." I have tried with both PHPMyAdmin2.3 and...
13
by: Dan V. | last post by:
How do I create a one line text file with these control codes? e.g.: 144 = 0x90 and 147 = 0x93? I am trying to create a one line text file with these characters all one one row with no spaces. ...
2
by: sympatico | last post by:
hi, pls help me slove this problem. i am a newbie in using asp.net and SQL server 2000 and my problem for now is to pass data to a textfile and i have fail to do so. the way i did it was i used...
8
by: MLH | last post by:
Am trying to import 20,000+ lines of text in a file FTP'd from a UNIX platform to windows via FTP session in a DOS box. About 2000 records have multiple lines in them separated by CRLF's. ...
13
by: DH | last post by:
Hi, I'm trying to strip the html and other useless junk from a html page.. Id like to create something like an automated text editor, where it takes the keywords from a txt file and removes them...
2
by: monkeyinquisition | last post by:
Hi All, I'm trying to read data from a text file into an array. The text file (a test) looks like this: 1 2 3 4 5 6 7 8
2
by: Naha | last post by:
Hi Guys, Does anyone know how to display data from a text file onto a webpage? So far I have opened and read the data in the text file and printed it out on command line, but I am having...
7
by: jetaw03 | last post by:
guys, can you help me to get data from a text file and putting it in an excel file? my programming language is visual basic 6.0 here is a sample data from the text file: ...
1
by: okd | last post by:
I am transfering data from a text file which is in uniccode format to a table . I am getting below error. SQLState = 22005, NativeError = 0 Error = Invalid character value for cast specification...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.