| | |
delete a list in text file
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jul 2006
Posts: 608
Reputation:
Solved Threads: 150
Ah. Here's a stab:
Jeff
Python Syntax (Toggle Plain Text)
def change_time(record, new_time): name,id,time = record.split('|') time = str(new_time) record = '|'.join((name,id,time)) + '|' return record
Jeff
Last edited by jrcagle; Mar 29th, 2007 at 10:18 pm.
•
•
Join Date: Mar 2007
Posts: 6
Reputation:
Solved Threads: 0
#!/usr/bin/python
import string
import re
import random
import sys
import time
import fileinput
tn = time.time()
t = str(tn)
ss = "5571"
lv = open("/usr/local/https/data/session1.txt","r")
lvr = lv.readlines()
l_lvr = len(lvr)
#print lvr
l_list = []
for i in range(l_lvr):
test = re.split('\|', lvr[i])
l_list.append(test)
#print l_list
for u,s,t,sp in l_list:
if s == ss:
so_list = [u, s, t, sp]
u = string.join(so_list)
#print f
s_list = u,s,t,sp
e = so_list[2]
print e
to = s_list[2]
#print st_to
too = float(to)
f = tn - too
if f < 3600:
so_list[2] = str(tn)
sn_list = so_list
print sn_list
g =string.join(sn_list)
print g
for line in fileinput.input('/usr/local/https/data/session1.txt', inplace=1):
line = line.replace(u, g)
sys.stdout.write(line)
sys.exit()
else:
#return 1
print "kK"
sys.exit()
print "no"
file line is not replacing with new one suggest
import string
import re
import random
import sys
import time
import fileinput
tn = time.time()
t = str(tn)
ss = "5571"
lv = open("/usr/local/https/data/session1.txt","r")
lvr = lv.readlines()
l_lvr = len(lvr)
#print lvr
l_list = []
for i in range(l_lvr):
test = re.split('\|', lvr[i])
l_list.append(test)
#print l_list
for u,s,t,sp in l_list:
if s == ss:
so_list = [u, s, t, sp]
u = string.join(so_list)
#print f
s_list = u,s,t,sp
e = so_list[2]
print e
to = s_list[2]
#print st_to
too = float(to)
f = tn - too
if f < 3600:
so_list[2] = str(tn)
sn_list = so_list
print sn_list
g =string.join(sn_list)
print g
for line in fileinput.input('/usr/local/https/data/session1.txt', inplace=1):
line = line.replace(u, g)
sys.stdout.write(line)
sys.exit()
else:
#return 1
print "kK"
sys.exit()
print "no"
file line is not replacing with new one suggest
•
•
Join Date: Aug 2008
Posts: 1
Reputation:
Solved Threads: 0
Okay im lost here...this is joining , how does remove the time field and leave the rest in tact...
•
•
•
•
Ah. Here's a stab:
Python Syntax (Toggle Plain Text)
def change_time(record, new_time): name,id,time = record.split('|') time = str(new_time) record = '|'.join((name,id,time)) + '|' return record
Jeff
•
•
Join Date: Dec 2006
Posts: 1,028
Reputation:
Solved Threads: 289
First, here is the way I __think__ your code looks when properly formatted. As you can see, it takes way too much time to try and fiqure out the code. so you are not getting any responses. Just click the "#" icon above the posting box and put your program between the code statements. The basic format of the program would be The data is now in a new file. You now move the new file to the old file's name or whatever you want.
Python Syntax (Toggle Plain Text)
#!/usr/bin/python ##import string ## string is deprecated import re import random import sys import time import fileinput tn = time.time() t = str(tn) ss = "5571" lv = open("/usr/local/https/data/session1.txt","r") lvr = lv.readlines() l_lvr = len(lvr) #print lvr l_list = [] for i in range(l_lvr): test = re.split('\|', lvr[i]) l_list.append(test) #print l_list for u,s,t,sp in l_list: if s == ss: so_list = [u, s, t, sp] u = string.join(so_list) #print f s_list = u,s,t,sp e = so_list[2] print e to = s_list[2] #print st_to too = float(to) f = tn - too if f < 3600: so_list[2] = str(tn) sn_list = so_list print sn_listg =string.join(sn_list) print g for line in open('/usr/local/https/data/session1.txt', "r"): line = line.replace(u, g) sys.stdout.write(line) sys.exit() ##--- don't know where the else goes else: #return 1 print "kK" sys.exit() print "no"
Python Syntax (Toggle Plain Text)
fp = open(file_name, "r") fp_out = open(file_name+".new", "w") for rec in fp: u,g = function_to_calculate_u_g_for this record() ## split the input rec into name, id, time, etc ## write to ".new" file fp_out.write("%s %s %s %s\n" % (name, id, u, g)) fp_out.close()
![]() |
Similar Threads
- How to delete a row in text file? (Visual Basic 4 / 5 / 6)
- to delete contents of a text file (Java)
- to delete contents of a text file (Java)
Other Threads in the Python Forum
- Previous Thread: Please help !
- Next Thread: Python Intrepeter in programs
| Thread Tools | Search this Thread |
address anydbm app beginner changecolor cipher class conversion coordinates corners curves definedlines development dictionary dynamic events examples excel feet file float format ftp function generator getvalue gui handling homework images import input ip java keycontrol line linux list lists loan loop maintain matching maze millimeter mouse mysqldb number numbers output parsing path permissions port prime programming projects py2exe pygame pymailer python queue random rational raw_input recursion recursive scrolledtext searchingfile shebang slicenotation split string strings table terminal text thread threading time tkinter tlapse tooltip tuple tutorial type ubuntu unicode url urllib urllib2 valueerror variable variables vigenere web windows wx.wizard wxpython xlwt






