473,385 Members | 1,356 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,385 software developers and data experts.

problem for write and delete a line in text file

Hi all,
my problem is delete a line in a text file.

the file contain -n line with user, userCode.
i want to replace the line with user is egual at user, and/or insert new line but delete the line like user.
i'm sorry for my english :D

if user exist delete a line of the user and insert new line.
if not exist insert new line.
possibility to delete a line.

please :D

Expand|Select|Wrap|Line Numbers
  1. user="myname"
  2. userCode="1987987981"
  3. f = open('userCodes.txt', 'r+')
  4. for line in f:
  5.  
  6.     if user in line:
  7.         print 'prima ' + str(line)
  8.         line = line.replace(line, user + ',' + str(userCode) + '\n')
  9.         f.write('\n' +str(line))
  10.         print 'dopo ' + str (line)
Mar 30 '07 #1
4 5534
This code is ok for my work.

Expand|Select|Wrap|Line Numbers
  1. user="myname"
  2. userCode="1987987981"
  3. import re
  4. import fileinput, string, sys
  5. ##f = open('userCodes.txt', 'rw')
  6.  ##print f
  7. ##f = open('userCodes.txt', 'r+')
  8. f= file('userCodes.txt')
  9. ##print f
  10.     ###scorro le linee del file
  11. newlines=[]
  12. for line in f:
  13.             if user in line:  
  14.                 print 'prima raplace ' + str(line)
  15.                 line = line.replace(line, user + ',' + str(userCode) + '\n')
  16.                 print 'dopo replace ' + str(line)
  17.             newlines.append(line)
  18. outfile=file('userCodes.txt','w')
  19. outfile.writelines(newlines)
Mar 31 '07 #2
ghostdog74
511 Expert 256MB
This code is ok for my work.

Expand|Select|Wrap|Line Numbers
  1. user="myname"
  2. userCode="1987987981"
  3. import re
  4. import fileinput, string, sys
  5. ##f = open('userCodes.txt', 'rw')
  6.  ##print f
  7. ##f = open('userCodes.txt', 'r+')
  8. f= file('userCodes.txt')
  9. ##print f
  10.     ###scorro le linee del file
  11. newlines=[]
  12. for line in f:
  13.             if user in line:  
  14.                 print 'prima raplace ' + str(line)
  15.                 line = line.replace(line, user + ',' + str(userCode) + '\n')
  16.                 print 'dopo replace ' + str(line)
  17.             newlines.append(line)
  18. outfile=file('userCodes.txt','w')
  19. outfile.writelines(newlines)
a little refinement
Expand|Select|Wrap|Line Numbers
  1. user="myname"
  2. userCode="1987987981"
  3. for line in open('userCodes.txt'):
  4.             line = line.strip() 
  5.             if user in line:                  
  6.                 line = line.replace(line, user + ',' + str(userCode) + '\n')
  7.                 print line
  8.  
you are only generating one output file right? you can save some code by just printing to stdout. then when you execute the script on your OS ,
Expand|Select|Wrap|Line Numbers
  1. # python yourscript.py > output.txt
  2.  
Apr 1 '07 #3
Thank's ghostdog74,
i have multiple code to write in file.txt when the user change or delete your accont.
In the test user and userCodes, is defined for semplify my work, but in the real script user and userCodes appartein at the user logged in.
If the user not exist in database also new line in the file .

Again sorry for my english.
Apr 1 '07 #4
ghostdog74
511 Expert 256MB
Thank's ghostdog74,
i have multiple code to write in file.txt when the user change or delete your accont.
In the test user and userCodes, is defined for semplify my work, but in the real script user and userCodes appartein at the user logged in.
If the user not exist in database also new line in the file .

Again sorry for my english.
sorry don't understand, but anyway, to output to different files in your script, use the method of opening files with "a" or "w" and write the data to it
eg
Expand|Select|Wrap|Line Numbers
  1. f = open("file","a")
  2. f.write("a string\n")
  3. f.close()
  4.  
or just simply
Expand|Select|Wrap|Line Numbers
  1. open("file","a').write("a string\n")
  2.  
Apr 2 '07 #5

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

Similar topics

5
by: Juho Saarikko | last post by:
I made a Python script which takes Usenet message bodies from a database, decodes uuencoded contents and inserts them as Large Object into a PostGreSQL database. However, it appears that the to...
1
by: Andrew | last post by:
Hey all, Working on revamping our Intranet here and making use of the LDPA, Active Directory, Directory Services, etc. that .Net provides. I am still fairly new on this subject, so the problem...
2
by: DC | last post by:
The Code <%@ import namespace="System" %> <%@ import namespace="System.Web" %> <%@ import namespace="System.Web.UI" %> <%@ import namespace="System.Web.UI.HtmlControls" %> <%@ import...
9
by: rn5a | last post by:
A Form has a DataGrid which displays records from a SQL Server 2005 DB table. Users can modify the records using this DataGrid for which I am using EditCommandColumn in the DataGrid. This is the...
0
by: JACompute | last post by:
I have an invoicing system that uses a VB6 program to post new invoice entries to a centralized Vendor management system (also in VB6), via a Web server and some ASP code. The system has been in...
0
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
3
by: Buddy Home | last post by:
Hello, I'm trying to upload a file programatically and occasionally I get the following error message. Unable to write data to the transport connection: An established connection was aborted...
19
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
2
by: swethak | last post by:
Hi, I am getting the problem the problem with google map in Internet Explorer. This map worked fine in mozilla . When i opened the same map in Internet Explorer i am getting the error...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...

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.