Python Forum
Iterate 2 large text files across lines and replace lines in second file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Iterate 2 large text files across lines and replace lines in second file
#5
Let's take your example. Your first file looks like this (od output).

0000000    a   d  \n   1   a  \n   2   b  \n   3   c  \n   .   .   .  \n
You say you want it to look like this afterward:

0000000    1   0  \n   0  \n   2   b  \n   4   5  \n   .   .   .  \n
See that the shorter second line means you're going to have to rewrite every byte that follows in the file since you've shifted the positions. You can't rewrite just the line that has changed. That's the advantage of a database with fixed-length fields. They can be rewritten in place cheaply.

Since you're already reading and writing every byte, you might as well use another file. There's no additional I/O cost, just disk space.

You could potentially rewrite every byte in the first file by reading a block, and writing the new block. But that means during the operation your file is inconsistent. If the program were to crash, you'd have a file that was half old and half new.

If you leave them the same length, then it should be possible to a replacement write. Uncommon, but we should be able to find an example.
Reply


Messages In This Thread
RE: Iterate 2 large text files across lines and replace lines in second file - by bowlofred - Aug-10-2020, 03:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace values in Yaml file with value in dictionary PelleH 0 168 Jun-12-2024, 02:40 PM
Last Post: PelleH
  speed up getting embedding from bert model for large set of text veda 7 491 May-27-2024, 08:28 AM
Last Post: Pedroski55
  Print the next 3 lines knob 3 368 May-22-2024, 12:26 PM
Last Post: andraee
  Working with group of lines knob 1 331 May-21-2024, 07:21 AM
Last Post: Gribouillis
  Line graph with two superimposed lines sawtooth500 4 524 Apr-02-2024, 08:56 PM
Last Post: sawtooth500
  replace text in a txt cartonics 19 2,690 Jan-30-2024, 06:58 AM
Last Post: Athi
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 2,276 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Replace a text/word in docx file using Python Devan 4 4,506 Oct-17-2023, 06:03 PM
Last Post: Devan
  How to insert Dashed Lines in between Rows of a tabulate output Mudassir1987 0 616 Sep-27-2023, 10:09 AM
Last Post: Mudassir1987
  Need to replace a string with a file (HTML file) tester_V 1 928 Aug-30-2023, 03:42 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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