By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,001 Members | 1,176 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,001 IT Pros & Developers. It's quick & easy.

replace string in a file

P: n/a
Hi,
I've got this code :

cb = open("testfile", "r+")
f = cb.readlines()
for line in f:
rx = re.match(r'^\s*(\d+).*', line)
if not rx:
continue
else:
serial = rx.group(1)
now = time.time()
today = time.strftime('%Y%m%d00', time.localtime(now))
todayVal, serialVal = long(today), long(serial)
if todayVal <= serialVal:
todayVal = serialVal + 1
else:
todayVal += 1
line = string.replace(line, serial, "%d" %todayVal)
cb.write(line + "\n")
print 'Updated serial from "%s" to "%d"' % ( serial, todayVal )
break
cb.close()

I was expecting to replace the old value (serial) with the new one
(todayVal). Instead, this code *adds* another line below the one found...

How can I just replace it?

Thanks for your help :-)
Mar 15 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Mar 15, 3:54*pm, Unknown <cantabile...@wanadoo.frwrote:
Hi,
I've got this code :

cb = open("testfile", "r+")
f = cb.readlines()
for line in f:
* * rx = re.match(r'^\s*(\d+).*', line)
* * if not rx:
* * * * continue
* * else:
* * * * serial = rx.group(1)
* * * * now = time.time()
* * * * today = time.strftime('%Y%m%d00', time.localtime(now))
* * * * todayVal, serialVal = long(today), long(serial)
* * * * if todayVal <= serialVal:
* * * * * * todayVal = serialVal + 1
* * * * else:
* * * * * * todayVal += 1
* * * * line = string.replace(line, serial, "%d" %todayVal)
* * * * cb.write(line + "\n")
* * * * print 'Updated serial from "%s" to "%d"' % ( serial, todayVal )
* * * * break * * *
cb.close()

I was expecting to replace the old value (serial) with the new one
(todayVal). Instead, this code *adds* another line below the one found...

How can I just replace it?

Thanks for your help :-)
What you want to do is either 1. load everything up into a string,
replace
text, close file, reopen it with 'w' flag, write string to it. OR if
file is too big, you can read each line, replace, write to a temp
file,
then when done move the file over the old one.

I think there are technical reasons why file-reading and iteration api
are not very suitable for reading and writing at the same time. I
think
you'd have to set the filepointer back one line (it can be manipulated
by giving it bytes ahead/back, so that would be difficult in itself),
then delete the line and then write new string. It may be even harder
than
that, I'm sure someone can explain this much better, but you're far
better
off with using one of the 2 methods above.. HTH, -ak
Mar 15 '08 #2

P: n/a
Thanks, andrei. I'll try that.

Le Sat, 15 Mar 2008 14:25:21 -0700, andrei.avk a √©crit¬*:
What you want to do is either 1. load everything up into a string,
replace
text, close file, reopen it with 'w' flag, write string to it. OR if
file is too big, you can read each line, replace, write to a temp file,
then when done move the file over the old one.
Mar 15 '08 #3

P: n/a
On 15 Mar, 21:54, Unknown <cantabile...@wanadoo.frwrote:
I was expecting to replace the old value (serial) with the new one
(todayVal). Instead, this code *adds* another line below the one found...

How can I just replace it?
A file is a stream of bytes, not a list of lines. You can't just
replace a line with another, unless they have the exact same length.
You must rewrite the whole file to get it right.
Mar 17 '08 #4

P: n/a
On Mar 16, 10:35 pm, sturlamolden <sturlamol...@yahoo.nowrote:
On 15 Mar, 21:54, Unknown <cantabile...@wanadoo.frwrote:
I was expecting to replace the old value (serial) with the new one
(todayVal). Instead, this code *adds* another line below the one found...
How can I just replace it?

A file is a stream of bytes, not a list of lines. You can't just
replace a line with another, unless they have the exact same length.
You must rewrite the whole file to get it right.
An example: looks for all 'junk*.txt' files in current directory and
replaces in each line the string 'old' by the string 'new'

<code>
import os, glob, fileinput

allfiles = glob.glob(os.getcwd() + '\\junk*.txt') # makes absolute
paths
findstr = 'old'
replstr = 'new'

countlinesfound = 0
for line in fileinput.input(allfiles,inplace=1):
if line.find(findstr) != -1:
line = line.replace(findstr,replstr) # old string , new
string
countlinesfound += 1
print line, # this writes line back to the file

print countlinesfound
</code>

I found something similar in a tutorial when I started to learn
Python, but I don't remember which one.

Josef
Mar 17 '08 #5

P: n/a
Le Mon, 17 Mar 2008 09:03:07 -0700, joep a √©crit¬*:
An example: looks for all 'junk*.txt' files in current directory and
replaces in each line the string 'old' by the string 'new'
Josef
Works like a charm. Many thanks for the example Josef :-)
Mar 18 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.