Python Forum
Last record in file doesn't write to newline
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Last record in file doesn't write to newline
#1
Apologies in advance for something that I'm sure you all have encountered before - but I cant find an answer!

The following code happily writes each line as a newline in the output file, unless the difference found is on the last line(I'm using difflib.Differ).
Hope you can see but the final '? ^' of each input file should be on a newline. I did try adding a newline (\n) to my write statement, but that just inserted every other line as spaces in the output

I'm sure there are many other ways to do this, but I'm just looking to understand what is the cause of the issue and the resolution - I suppose I could test if its the last record in the file and treat it differently, but that seems a bit rubbish.

for line in difference.compare(a, b):
   f3.write(line)  
- dhx22
? ^
+ dhj22
? ^
- d23y
? -

- aaaad34? ^
+ aabad34? ^
Reply
#2
I don't have the problem you describe using difflib. Could you provide a little more context please. What are a, b and difference? Could you post a complete example?

My guess is that a and b are strings read from two files. All the lines end with a newline except the last. If that's the problem, you can hardly fault difflib that your input files don't end with an empty line.

You could do something like this, letting you control where newlines appear in the output file:
from difflib import Differ

with open("text.txt", "w") as file, open("a.txt", "r") as a, open("b.txt", "r") as b:
    for x in Differ().compare(a.readlines(), b.readlines()):
        print(x.strip(), file=file)
Reply
#3
Thanks for getting back to me @deanhystad. I think your hunch must be correct as your revised code works as expected.
Reply
#4
From the difflib documentation:
Quote:compare(a, b)
Compare two sequences of lines, and generate the delta (a sequence of lines).

Each sequence must contain individual single-line strings ending with newlines. Such sequences can be obtained from the readlines() method of file-like objects. The delta generated also consists of newline-terminated strings, ready to be printed as-is via the writelines() method of a file-like object.
It may be slightly inconvenient, but at least it tells you up front.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  What does .flush do? How can I change this to write to the file? Pedroski55 3 229 Apr-22-2024, 01:15 PM
Last Post: snippsat
  write to csv file problem jacksfrustration 11 1,551 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,471 Nov-09-2023, 10:56 AM
Last Post: mg24
  Facing issue in python regex newline match Shr 6 1,321 Oct-25-2023, 09:42 AM
Last Post: Shr
  How do I stream and record at the same time with arducam? traderjoe 0 471 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  How do I read and write a binary file in Python? blackears 6 6,701 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,114 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,631 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 10,050 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  How to write in text file - indented block Joni_Engr 4 6,475 Jul-18-2022, 09:09 AM
Last Post: Hathemand

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020