I am brand new to Python. I am reading text from a file & replacing a word. I simply want to output the same two lines, with the word replacement, where the first line ends with a newline.
ADAMS, Ernie, 166 Winterstreamrose Way
NEWLINE, None, 1 Nomorenewlines Street
My test code is:
# readFileLines.py --- testing file line reading, replacing text & dealing with newlines
with open("line.txt") as f:
for line in f:
for word in line.split():
if word == 'Way':
line = line.replace("Way", "Street")
print(line)
Output:
ADAMS, Ernie, 166 Winterstreamrose Street
NEWLINE, None, 1 Nomorenewlines Street
Why do I get an extra newline between the output lines? I note, that like in line.txt, there is no newline after the second line of output.
Any suggestions appreciated.