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 5534
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...
|
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...
|
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...
|
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...
|
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...
|
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: 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: 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,...
|
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...
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |