This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: replacements function corrupts file
Type: behavior Stage: resolved
Components: Versions: Python 3.6
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: dutch58, terry.reedy, zach.ware
Priority: normal Keywords:

Created on 2020-11-06 18:37 by dutch58, last changed 2022-04-11 14:59 by admin. This issue is now closed.

File name Uploaded Description Edit
IBC CAP.txt dutch58, 2020-11-06 18:37 flat file of text with a few populated columns
Messages (2)
msg380466 - (view) Author: John Dutcher (dutch58) Date: 2020-11-06 18:37
If the file below 0f 239 records is processed by the code below it throws no errors and writes the output...but the output has 615 records instead of 239 seemingly having written any revised ones three times instead of only once.

replacements = {'/1':'/01', '9/':'09/', '7/':'07/'}
file2 = open(r"c:\users\liddvdp\desktop\IBC CAP OUT.txt", "w")
with open(r"c:\users\liddvdp\desktop\IBC CAP.txt", "r") as reader:
         for line in reader:
             for src, target in replacements.items():
                 line = line.replace(src, target)
msg380468 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2020-11-06 19:07
That's because you're writing an output line once per replacement rather than once per input line.

For usage questions such as this, you'll be better off on a forum such as, the mailing list, or the Python Discord community (
Date User Action Args
2022-04-11 14:59:37adminsetgithub: 86445
2020-11-06 19:07:31zach.waresetcomponents: - IDLE
2020-11-06 19:07:09zach.waresetstatus: open -> closed

assignee: terry.reedy ->

nosy: + zach.ware
messages: + msg380468
resolution: not a bug
stage: resolved
2020-11-06 18:37:29dutch58create