Mailing List Archive

Save-to-file code not quite working completely
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... (-:


=======================================================
Footnote:
?logomachist?- someone given to disputes over words. logomach.
controversialist, disputant, eristic - a person who disputes; who is good at
or enjoys controversy.







--
https://mail.python.org/mailman/listinfo/python-list
Re: Save-to-file code not quite working completely [ In reply to ]
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
Re: Save-to-file code not quite working completely [ In reply to ]
On 09/08/2020 08: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.
...
> 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?


To be a logomach, let's talk about "update":-
May I advise that a 'good practice' would be to create a new file, and
thus be able to (also) maintain the old version as a 'backup'.
(also: advantage when debugging/testing logic!)

The pattern is to read the 'old' file, line-by-line. If there is a
key-match, ignore the read record, writing the new data in its stead.
If there is not a match, copy the input-data into an output record.

The major addition will be the need to rename the 'old file', ensuring
logic to handle any "clash" (based upon whatever security
requirements/numbers of generations - which it would appear don't
currently exist).

The above assumes that the program will either only ever update a single
record per execution-run; or that the logic collects a list of changes
to the file which will be saved to the file, as above.


Further, databases have been designed for this sort of partial update
scenario. If/when updates become too frequent, or the file becomes
extensive, recommend you look at using an RDBMS instead of using this
'flat-file' approach...
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list
Re: Save-to-file code not quite working completely [ In reply to ]
On 10/08/2020 05:23, Dennis Lee Bieber wrote:
> On Sun, 9 Aug 2020 11:50:51 +1200, dn via Python-list
> <python-list@python.org> declaimed the following:
>> To be a logomach, let's talk about "update":-
>> May I advise that a 'good practice' would be to create a new file, and
>> thus be able to (also) maintain the old version as a 'backup'.
>> (also: advantage when debugging/testing logic!)
>
> Per the documentation, this is exactly what the "inplace=True" does! It
> renames the original file, then writes the output under the original name.
> Keeping the backup after processing requires providing an explicit
> "backup=.ext" option, otherwise it uses .bak and deletes the file when
> processing completes.


Apologies for incomplete answer.

I have finally remembered why I (have long) steer-clear of fileinput:
a) criticism from Martijn Pieters
https://www.zopatista.com/python/2013/11/26/inplace-file-rewriting/
b) I can't recall 'my' last application requiring/desiring a flat-file
that wasn't JSON or YAML.

The above article is (2013) old. I notice that whilst the github
received a trivial update one month back, that the PyPI entry is almost
two years old.

I (am biased, admittedly) still maintain that for anything more than the
trivial, a direct-access mechanism is the tool-for-the-job, and because
I think they are 'easy', an RDBMS. YMMV!
--
Regards =dn
--
https://mail.python.org/mailman/listinfo/python-list