delete a list in text file

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 6
Reputation: nitinloml is an unknown quantity at this point 
Solved Threads: 0
nitinloml nitinloml is offline Offline
Newbie Poster

delete a list in text file

 
0
  #1
Mar 29th, 2007
delete a list in text file well i m having a text file which contain time,user name & id, now if i want to modify the time without affecting the id & user name is it possible?
or how i delete that list & enter a new list in that text file
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,221
Reputation: bumsfeld will become famous soon enough bumsfeld will become famous soon enough 
Solved Threads: 137
bumsfeld's Avatar
bumsfeld bumsfeld is offline Offline
Nearly a Posting Virtuoso

Re: delete a list in text file

 
0
  #2
Mar 29th, 2007
It would be nice, if you could give us an actual example, so we don't have to guess too wildly!
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 6
Reputation: nitinloml is an unknown quantity at this point 
Solved Threads: 0
nitinloml nitinloml is offline Offline
Newbie Poster

Re: delete a list in text file

 
0
  #3
Mar 29th, 2007
puru|8346|(2007, 3, 26, 0, 36, 59, 0, 85, 0)|
admin|5194|(2007, 3, 28, 3, 2, 21, 1, 86, 0)|
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 6
Reputation: nitinloml is an unknown quantity at this point 
Solved Threads: 0
nitinloml nitinloml is offline Offline
Newbie Poster

Re: delete a list in text file

 
0
  #4
Mar 29th, 2007
puru|8346|(2007, 3, 26, 0, 36, 59, 0, 85, 0)|
admin|5194|(2007, 3, 28, 3, 2, 21, 1, 86, 0)|
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 608
Reputation: jrcagle is on a distinguished road 
Solved Threads: 150
jrcagle jrcagle is offline Offline
Practically a Master Poster

Re: delete a list in text file

 
0
  #5
Mar 29th, 2007
Ah. Here's a stab:

  1. def change_time(record, new_time):
  2.  
  3. name,id,time = record.split('|')
  4. time = str(new_time)
  5. record = '|'.join((name,id,time)) + '|'
  6. return record

Jeff
Last edited by jrcagle; Mar 29th, 2007 at 10:18 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 6
Reputation: nitinloml is an unknown quantity at this point 
Solved Threads: 0
nitinloml nitinloml is offline Offline
Newbie Poster

Re: delete a list in text file

 
0
  #6
Mar 30th, 2007
#!/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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1
Reputation: icsoka is an unknown quantity at this point 
Solved Threads: 0
icsoka icsoka is offline Offline
Newbie Poster

Re: delete a list in text file

 
0
  #7
Dec 18th, 2008
Okay im lost here...this is joining , how does remove the time field and leave the rest in tact...


Originally Posted by jrcagle View Post
Ah. Here's a stab:

  1. def change_time(record, new_time):
  2.  
  3. name,id,time = record.split('|')
  4. time = str(new_time)
  5. record = '|'.join((name,id,time)) + '|'
  6. return record

Jeff
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,028
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 289
woooee woooee is offline Offline
Veteran Poster

Re: delete a list in text file

 
0
  #8
Dec 19th, 2008
First, here is the way I __think__ your code looks when properly formatted.
  1. #!/usr/bin/python
  2. ##import string ## string is deprecated
  3. import re
  4. import random
  5. import sys
  6. import time
  7. import fileinput
  8.  
  9. tn = time.time()
  10. t = str(tn)
  11. ss = "5571"
  12. lv = open("/usr/local/https/data/session1.txt","r")
  13. lvr = lv.readlines()
  14. l_lvr = len(lvr)
  15. #print lvr
  16. l_list = []
  17. for i in range(l_lvr):
  18.  
  19. test = re.split('\|', lvr[i])
  20. l_list.append(test)
  21. #print l_list
  22. for u,s,t,sp in l_list:
  23. if s == ss:
  24. so_list = [u, s, t, sp]
  25. u = string.join(so_list)
  26. #print f
  27. s_list = u,s,t,sp
  28. e = so_list[2]
  29. print e
  30. to = s_list[2]
  31. #print st_to
  32. too = float(to)
  33. f = tn - too
  34. if f < 3600:
  35. so_list[2] = str(tn)
  36. sn_list = so_list
  37. print sn_listg =string.join(sn_list)
  38. print g
  39. for line in open('/usr/local/https/data/session1.txt', "r"):
  40. line = line.replace(u, g)
  41. sys.stdout.write(line)
  42. sys.exit()
  43.  
  44. ##--- don't know where the else goes
  45. else:
  46. #return 1
  47. print "kK"
  48. sys.exit()
  49. print "no"
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
  1. fp = open(file_name, "r")
  2. fp_out = open(file_name+".new", "w")
  3. for rec in fp:
  4. u,g = function_to_calculate_u_g_for this record()
  5. ## split the input rec into name, id, time, etc
  6. ## write to ".new" file
  7. fp_out.write("%s %s %s %s\n" % (name, id, u, g))
  8. fp_out.close()
The data is now in a new file. You now move the new file to the old file's name or whatever you want.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC