473,327 Members | 1,952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

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
Mar 27 '07 #1
7 3018
bartonc
6,596 Expert 4TB
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
If you provide a sample of you text file, we may be able to work out a way to parse out the entry, modify it and put it back together again. It will help if you also provide any code that you have in progress so that your own style of coding and naming will be reflected in future posts.
Mar 27 '07 #2
puru|8346|(2007, 3, 26, 0, 36, 59, 0, 85, 0)|
admin|5194|(2007, 3, 28, 3, 2, 21, 1, 86, 0)|
~
~
this is my text file
Mar 27 '07 #3
bartonc
6,596 Expert 4TB
Merging threads:

puru|8346|(2007, 3, 26, 0, 36, 59, 0, 85, 0)|
admin|5194|(2007, 3, 28, 3, 2, 21, 1, 86, 0)|
~
~
this is my text file
Ok, but you need to use the Reply button, not the Start a discussion button, in order to keep the conversation all in one place.
Mar 27 '07 #4
shonen
7
well this would be how I'd do it


Expand|Select|Wrap|Line Numbers
  1. x = open('nameoffilehere.txt','r')
  2.  
  3. holder=[]
  4. for l in x:
  5.    line = l.strip('\n')     #get rid of the pesky newline
  6.    line = line.split('|')   # i think you used | to split lines
  7.    holder+=[line]
  8.  
  9. x.close()
  10. #now the entire file is set up in a list
  11. #now you should be able to access your info similarly to this:
  12.  
  13. print holder[0][1]
  14.  
  15.  
I would slurp it into basically a list of lists and then iterate through that, and rewrite the file after i parsed it back together...

I'll let some of the more godly python coders, to tell you a better solution im sure (using less size and such)

just how i would do it and maybe get you on your way.

~shonen
Mar 27 '07 #5
ghostdog74
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. import fileinput
  2. for line in fileinput.FileInput("file", inplace=1): #inplace editing
  3.     line = line.strip("|\n").split("|") #you have a last "|" in every line
  4.     timestamp = line[2] ## modify your timestamp
  5.     print '|'.join(line) # add "|" to the end as necessary
  6.  
Mar 28 '07 #6
#!/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 value
Mar 30 '07 #7
bvdet
2,851 Expert Mod 2GB
#!/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 value
This is similar to ghostdog74's, but it creates a new file:
Expand|Select|Wrap|Line Numbers
  1. import time
  2.  
  3. fList = open('data_file').readlines()
  4.  
  5. outStr = ''
  6. for line in fList:
  7.     lineList = line.strip('|\n').split('|')
  8.     lineList[2] = str(time.localtime()) # set the time here
  9.     outStr += '%s\n' % '|'.join(lineList)
  10.  
  11. f = open('new_file', 'w')
  12. f.write(outStr.strip())
  13. f.close()
Mar 30 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Phil Powell | last post by:
print_r(is_file("$logPath/$logFileName")); // RETURNS 1 unlink("$logPath/$logFileName"); // RETURNS WARNING: PERMISSION DENIED This code should tell me that the file located at...
5
by: Jobs | last post by:
Hello All, I want to delete all files in a directory. I am making a backup copy of all files in the directories say c:\abc by reading and writing to a file. After making a backup copy I want to...
3
by: Paul | last post by:
Is it possible to delete a file on a client side machine using VB/JAVA script? My website allows the user to upload a file to the website and after that I would like to delete the file on their...
16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
1
by: apple | last post by:
i try to print image file in a directory using PrintDocument. It will raise printPage event to draw image to the printer. The file will be deleted after print and the directory will be checked...
0
by: isaac2004 | last post by:
im trying to make a page that reads simple user information and posts them to a table. the user can edit, delete and add new records, through a form on the page.using SQLs modifications like...
5
by: wo20051223 | last post by:
Deleting some files with C# fails with "Access to the path 'X' is denied". I have files copied from a CD that I burned (and not locked by a process) and a text file that I created in Windows...
3
by: Barkingmadscot | last post by:
I am stuck, i can workout how to remove lines from an array I have loading a text file (a Log), I know which lines a need, but the logs can be upto 30K sometimes bigger. I found trying to...
0
by: wolfsbane | last post by:
Alright, here it is I am trying to write a win32 app in VB 2005 to clean up user's profiles. everything works correctly except for the Delete("directory", True) statement. I get a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.