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
#11
Same as the sample code above. Instead of setting line1, line2 directly from the filehandle, set them from a readline().

line1 = fileA.readline()
line2 = fileB.readline()
while all(line1, line2):
    # do loop stuff here
    # at end of loop, read the lines again
    line1 = fileA.readline()
    line2 = fileB.readline()
Reply
#12
Ok so this is the code I have

with open("file1", 'r') as fileA, open("file2", 'r+') as fileB:
    line1 = fileA.readline()
    line2 = fileB.readline()
    line_start = fileB.tell()
    while all(line1, line2):
        if line1 == line2:
            replace = "00"
            fileB.seek(line_start)
            fileB.write(replace)
        line_start = fileB.tell()
        line1 = fileA.readline()
        line2 = fileB.readline()
and I get error
TypeError: all() takes exactly one argument (2 given)
Also not sure about the right order on tell() and readline()
Reply
#13
Yeah, I typed that up quickly. Probably better would be

with open("file1", 'r') as fileA, open("file2", 'r+') as fileB:
    line_start = fileB.tell() # want the current position before reading the line.
    line1 = fileA.readline()
    line2 = fileB.readline()
    while line1 and line2:
        if line1 == line2:
            replace = "00"
            fileB.seek(line_start)
            fileB.write(replace) # make sure you're writing the correct length here. Too long and you'll merge lines.  Too short and you'll leave old data behind.
        line_start = fileB.tell()
        line1 = fileA.readline()
        line2 = fileB.readline()
Reply
#14
Worked, needed a minor tweak to make it work for the very last line as well

with open("file1", 'r') as fileA, open("file2", 'r+') as fileB:
    line_start = fileB.tell()
    line1 = fileA.readline()
    line2 = fileB.readline()
    while line1 and line2:
        if line1 == line2:
            replace = "00"
            fileB.seek(line_start)
            fileB.write(replace) 
        line_start = fileB.tell()
        if line1 != line2: line1 = fileA.readline()
        line2 = fileB.readline()
Thanks!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Line graph with two superimposed lines sawtooth500 4 301 Apr-02-2024, 08:56 PM
Last Post: sawtooth500
  replace text in a txt cartonics 19 2,214 Jan-30-2024, 06:58 AM
Last Post: Athi
  Python and pandas: Aggregate lines form Excel sheet Glyxbringer 12 1,839 Oct-31-2023, 10:21 AM
Last Post: Pedroski55
  Replace a text/word in docx file using Python Devan 4 3,301 Oct-17-2023, 06:03 PM
Last Post: Devan
  How to insert Dashed Lines in between Rows of a tabulate output Mudassir1987 0 496 Sep-27-2023, 10:09 AM
Last Post: Mudassir1987
  Need to replace a string with a file (HTML file) tester_V 1 761 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  detect if two lines are crossing bast0s4 2 708 Aug-16-2023, 04:23 PM
Last Post: Gribouillis
  Converted EXE file size is too large Rajasekaran 0 1,507 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  What are these python lines for? What are tey doing? Led_Zeppelin 7 1,612 Feb-13-2023, 03:08 PM
Last Post: deanhystad
  azure TTS from text files to mp3s mutantGOD 2 1,695 Jan-17-2023, 03:20 AM
Last Post: mutantGOD

Forum Jump:

User Panel Messages

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