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 -
user="myname"
-
userCode="1987987981"
-
f = open('userCodes.txt', 'r+')
-
for line in f:
-
-
if user in line:
-
print 'prima ' + str(line)
-
line = line.replace(line, user + ',' + str(userCode) + '\n')
-
f.write('\n' +str(line))
-
print 'dopo ' + str (line)
4 5575
This code is ok for my work. -
user="myname"
-
userCode="1987987981"
-
import re
-
import fileinput, string, sys
-
##f = open('userCodes.txt', 'rw')
-
##print f
-
##f = open('userCodes.txt', 'r+')
-
f= file('userCodes.txt')
-
##print f
-
###scorro le linee del file
-
newlines=[]
-
for line in f:
-
if user in line:
-
print 'prima raplace ' + str(line)
-
line = line.replace(line, user + ',' + str(userCode) + '\n')
-
print 'dopo replace ' + str(line)
-
newlines.append(line)
-
outfile=file('userCodes.txt','w')
-
outfile.writelines(newlines)
This code is ok for my work. -
user="myname"
-
userCode="1987987981"
-
import re
-
import fileinput, string, sys
-
##f = open('userCodes.txt', 'rw')
-
##print f
-
##f = open('userCodes.txt', 'r+')
-
f= file('userCodes.txt')
-
##print f
-
###scorro le linee del file
-
newlines=[]
-
for line in f:
-
if user in line:
-
print 'prima raplace ' + str(line)
-
line = line.replace(line, user + ',' + str(userCode) + '\n')
-
print 'dopo replace ' + str(line)
-
newlines.append(line)
-
outfile=file('userCodes.txt','w')
-
outfile.writelines(newlines)
a little refinement -
user="myname"
-
userCode="1987987981"
-
for line in open('userCodes.txt'):
-
line = line.strip()
-
if user in line:
-
line = line.replace(line, user + ',' + str(userCode) + '\n')
-
print line
-
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 , -
# python yourscript.py > output.txt
-
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.
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 -
f = open("file","a")
-
f.write("a string\n")
-
f.close()
-
or just simply -
open("file","a').write("a string\n")
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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...
|
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...
|
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" %>
|
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()
|
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....
| |
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
|
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
|
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...
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
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();...
|
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...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |