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
  How can I write formatted (i.e. bold, italic, change font size, etc.) text to a file? JohnJSal 12 27,697 Feb-13-2025, 04:48 AM
Last Post: tomhansky
  How to write variable in a python file then import it in another python file? tatahuft 4 839 Jan-01-2025, 12:18 AM
Last Post: Skaperen
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,442 Oct-17-2024, 01:15 AM
Last Post: Winfried
  how to process 12 million record pymach 4 1,042 Aug-01-2024, 07:58 AM
Last Post: Pedroski55
  What does .flush do? How can I change this to write to the file? Pedroski55 3 1,274 Apr-22-2024, 01:15 PM
Last Post: snippsat
  write to csv file problem jacksfrustration 11 4,733 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 3,684 Nov-09-2023, 10:56 AM
Last Post: mg24
  Facing issue in python regex newline match Shr 6 5,784 Oct-25-2023, 09:42 AM
Last Post: Shr
  How do I stream and record at the same time with arducam? traderjoe 0 1,012 Oct-23-2023, 12:01 AM
Last Post: traderjoe
  How do I read and write a binary file in Python? blackears 6 23,588 Jun-06-2023, 06:37 PM
Last Post: rajeshgk

Forum Jump:

User Panel Messages

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