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
7 3018
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.
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
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.
well this would be how I'd do it -
x = open('nameoffilehere.txt','r')
-
-
holder=[]
-
for l in x:
-
line = l.strip('\n') #get rid of the pesky newline
-
line = line.split('|') # i think you used | to split lines
-
holder+=[line]
-
-
x.close()
-
#now the entire file is set up in a list
-
#now you should be able to access your info similarly to this:
-
-
print holder[0][1]
-
-
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
-
import fileinput
-
for line in fileinput.FileInput("file", inplace=1): #inplace editing
-
line = line.strip("|\n").split("|") #you have a last "|" in every line
-
timestamp = line[2] ## modify your timestamp
-
print '|'.join(line) # add "|" to the end as necessary
-
#!/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
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: - import time
-
-
fList = open('data_file').readlines()
-
-
outStr = ''
-
for line in fList:
-
lineList = line.strip('|\n').split('|')
-
lineList[2] = str(time.localtime()) # set the time here
-
outStr += '%s\n' % '|'.join(lineList)
-
-
f = open('new_file', 'w')
-
f.write(outStr.strip())
-
f.close()
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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....
|
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
| |