473,804 Members | 3,063 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem for write and delete a line in text file

3 New Member
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 5575
jeepers
3 New Member
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 Recognized Expert Contributor
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
jeepers
3 New Member
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 Recognized Expert Contributor
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
2726
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 last few bytes of uudecoded data are always mangled. Take a look of this hexdump output: Originals (decoded with Pan, each line is from a different file): 000c2c0 e1bf 00ff 2541 a9e4 a724 d9ff 0011a10 ff54 00d9 00093e0 fb4f a80d ffd9 c200 ffef...
1
4760
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 I have run into I am not sure how to fix, and really not sure what is causing it. Here's what is going on (test server - Windows 2003 Server): I have a page in a folder (under anonymous authentication in IIS6) that has a link on it that...
2
2966
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 namespace="System.Web.UI.WebControls" %> <%@ import namespace="System.Data" %> <%@ import namespace="System.Data.OleDb" %>
9
2734
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 code: <script runat="server"> Dim sqlConn As New SqlConnection(".....") Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs) If Not (Page.IsPostBack) Then FillDataGrid()
0
1460
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 production for some time now, but one pesky little problem keeps surfacing periodically. System Description: 1. For each new line on an invoice, a specially formatted query line is appended to an Access database along with a blank time/date field....
0
789
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 by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
3
14063
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 by the software in your host machine. Stack Trace at System.Net.Sockets.NetworkStream.Write(Byte buffer, Int32 offset, Int32
19
4785
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, sell price, and a taxable flag (a Y/N char) and then write this all out to a file (preferably just a plain old text file) and then read it in later so that I can keep a running inventory. The problem that I am running into is when I write to the...
2
3158
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 message as in alert box as " Internet Explorer cannot open the Internet site http://google.citycarrentals.com.au/viewalllocations.php . Operation aborted". It is working in Mozilla . Here i mentioned my code . I am facing this problem several...
0
9711
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10343
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10331
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9166
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7631
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6861
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5529
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3831
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.