Mailing List Archive

Mischief Managed: Save-to-file code not quite working completely
A simple solution is to know better how the operation works. (-:
I was thinking that the operation would search for the keyword (MSN) and replace the line. Instead of passing the key word, I passed the entire line to be replaced and it's correction..

def ReplaceLine(file,searchExp,replaceExp):
for line in fileinput.input(file, inplace=1):
#if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)

ReplaceLine("Specifications.txt",OldLine, NewLine)


Footnote:
If 666 is evil then
25.8 is the root of all evil.

-----Original Message-----
From: MRAB <python@mrabarnett.plus.com>
Sent: Saturday, August 8, 2020 9:08 PM
To: Steve <Gronicus@SGA.Ninja>
Subject: Re: Save-to-file code not quite working completely

On 2020-08-09 00:51, Steve wrote:
> I don't see that. What I do see is that it placing the new string but
> leaving most of the old one.

That's how the .replace method works. It searches for matches and replaces any matched portion(s) with the replacement string. It leaves the rest of the string alone.

You told it to replace all occurrences of "MSN" with something else. It did that. It didn't make any other changes.

What do you want it to do?

If you want it to replace the line that contains "MSN" with the new line, but retain the number, then you need to write code to do that.
Something like this:

if searchExp in line:
# Retain the number from the line.
line = line.split()[0] + ' ' + replaceExp

> -----Original Message-----
> From: Python-list <python-list-bounces+gronicus=sga.ninja@python.org>
> On Behalf Of MRAB
> Sent: Saturday, August 8, 2020 6:02 PM
> To: python-list@python.org
> Subject: Re: Save-to-file code not quite working completely
>
> On 2020-08-08 21:51, Steve wrote:
> >
> > I have a file containing specifications. My .py program can read and
> > manipulate the data but I cannot seem to get it to update the
> > original file properly.
> >
> > The code is simple and works except that the original line remains
> > in the file after the new updated one has been added.
> >
> > My code:
> > =======================================================
> >
> > import fileinput
> > import sys
> >
> > def ReplaceLine(file,searchExp,replaceExp):
> > for line in fileinput.input(file, inplace=1):
> > # if searchExp in line:
> > line = line.replace(searchExp,replaceExp) #.rstrip()
> > sys.stdout.write(line)
> >
> > NewLine = "MSN Monitor Serial Number: 111111111-T4464
> ##
> > \n "
> > ReplaceLine("Specifications.txt","MSN", NewLine)
> > print()
> > PauseHere = input("Paused")
> > =====================================
> > The text file:
> > 1 MSN Monitor Serial Number: 111111111-T4464 ##
> > Monitor Serial Number: 88888888-T4464 ##
> > 2 TLN TestStrip Lot Number: 45001 82624 ##
> > 3 SED Strip Expire Date: 2021-02-28 ##
> > 4 SEC Sensor Sequence Code: 68 ##
> > 5 SCN Sensor Code Number: F95 ##
> > 6 SEN Sensor Serial Number: 0M000APJYWM ##
> > 7 SDE Sensor Date to Expire: 2020-12-31 ##
> > 8 SDN Sensor Day Number: 1 ##
> > 9 DTD Data Time Date Fri Aug 07, 2020 21:30 ##
> > =====================================
> > That second line shown was the original line for MSN. The
> > replacement line should have replaced the original line not just get
> > added to the file. It should have been replaced. What is in line 2,
> > should have "1 MSN" at the beginning but that somehow disappeared.
> >
> > So close, so simple...
> > How do I fix this?
> > Steve
> >
> > P.S. I read to add ".rstrip()" but that messed things even more... (-:
> >
> In the .replace line you're asking it to replace any occurrence of "MSN"
> in the line with a new string.
>
> It's doing that.
>
> I'll add <<...>> around the replacement to make its position clearer:
>
> >>> line = " 1 MSN Monitor Serial Number: 111111111-T4464 ##\n"
> >>> line.replace("MSN", "<<MSN Monitor Serial Number: 111111111-T4464
> ##\n >>")
> ' 1 <<MSN Monitor Serial Number: 111111111-T4464 ##\n
> >> Monitor Serial Number: 111111111-T4464 ##'

--
https://mail.python.org/mailman/listinfo/python-list