Python Forum
Replace lines from one .txt file to another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Replace lines from one .txt file to another
#11
How i can repeat python commands and change some parameter every time in repeat? I just searching and seems that here is not to easy way to do this?

Now i'am very close the finish this, but i do not wanna write this almost same command about 80 times with some another parameters.

I mean this:  w and d +values change:
w=50
d=56 

#3
linesToCopy=range((w+16),(d+30))
with open('file1.txt') as f1, open('file3.txt', 'a') as f2:
   for i, line in enumerate(f1, 1):
       if i in linesToCopy: 
           f2.write(line)
           
#4        
linesToCopy=range((w+20),(d+20))
with open('file2.txt') as f1, open('file3.txt', 'a') as f2:
   for i, line in enumerate(f1, 1):
       if i in linesToCopy: 
           f2.write(line)
           
           
#5
linesToCopy=range((w+36),(d+52))
with open('file1.txt') as f1, open('file3.txt', 'a') as f2:
   for i, line in enumerate(f1, 1):
       if i in linesToCopy: 
           f2.write(line)
           
#6        
linesToCopy=range((w+42),(d+42))
with open('file2.txt') as f1, open('file3.txt', 'a') as f2:
   for i, line in enumerate(f1, 1):
       if i in linesToCopy: 
           f2.write(line)
           
#7
linesToCopy=range((w+58),(d+72))
with open('file1.txt') as f1, open('file3.txt', 'a') as f2:
   for i, line in enumerate(f1, 1):
       if i in linesToCopy: 
           f2.write(line)
           
#8        
linesToCopy=range((w+62),(d+62))
with open('file2.txt') as f1, open('file3.txt', 'a') as f2:
   for i, line in enumerate(f1, 1):
       if i in linesToCopy: 
           f2.write(line)         

#......80
And yes, there is some same cycle every 2/4 command/file1 and 2

Cycle is like:
#3. file1 command  w+16 d+30 
#4. file2 command  w+20 d+20 
#5. file1  command  w+36 d+52  (w+20 d+22 than before file1 command)
#6. file2 command  w+42 d+42  (w+22 d+22 than before file2 command)
#7. file1  command  w+58 d+72  (w+22 d+20 than before file1 command)
#8. file2 command  w+62 d+62  (w+20 d+20 than before file2 command)

#9. file1  command  w+78 d+94  (w+20 d+22 than before file1 command)
#10. file2 command  w+84 d+84  (w+22 d+22 than before file2 command)
#11. file1  command  w+98 d+114  (w+22 d+20 than before file1 command)
#12. file2 command  w+104 d+104  (w+20 d+20 than before file2 command)
Reply
#12
This is the purpose of "functions"
# define the function
def copyFileLines(fromFileName,toFileName,start,end):
    linesToCopy=range(start,end)
    with open(fromFileName) as fromFile, open(toFileName, 'a') as toFile:
        for i, line in enumerate(fromFile, 1):
            if i in linesToCopy: 
                toFile.write(line)
    
# use it:
fileName1='file1.txt'
fileName2='file2.txt'
fileName3='file3.txt'


copyFileLines(fileName1,fileName3, w+16,d+30)
copyFileLines(fileName2,fileName3, w+20,d+20)
copyFileLines(fileName1,fileName3, w+36,d+52)
copyFileLines(fileName2,fileName3, w+42,d+42)

# etc...
Note that you are opening/closing all the files each time, if they are big the performance is going to be awful. You can open the target file once for all before calling the functions (replace parameter with its name with a parameter that hold the open file object). There is likely a simpler logic (fixed intervals) that allows you top open the two source files once for all, especially if they are always read/copied in the forward direction. In which case your code would just be a short loop.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply
#13
(Jun-23-2017, 08:19 PM)Ofnuts Wrote: This is the purpose of "functions"
# define the function
def copyFileLines(fromFileName,toFileName,start,end):
    linesToCopy=range(start,end)
    with open(fromFileName) as fromFile, open(toFileName, 'a') as toFile:
        for i, line in enumerate(fromFile, 1):
            if i in linesToCopy: 
                toFile.write(line)
    
# use it:
fileName1='file1.txt'
fileName2='file2.txt'
fileName3='file3.txt'


copyFileLines(fileName1,fileName3, w+16,d+30)
copyFileLines(fileName2,fileName3, w+20,d+20)
copyFileLines(fileName1,fileName3, w+36,d+52)
copyFileLines(fileName2,fileName3, w+42,d+42)

# etc...
Note that you are opening/closing all the files each time, if they are big the performance is going to be awful. You can open the target file once for all before calling the functions (replace parameter with its name with a parameter that hold the open file object). There is likely a simpler logic (fixed intervals) that allows you top open the two source files once for all, especially if they are always read/copied in the forward direction. In which case your code would just be a short loop.

Files are small, how it possible to repeat those lines where "w" and "d" values are with fixed values?

Look my last post, there are same cycle every 4 blocks after #4 command line...
Reply
#14
(Jun-24-2017, 05:22 AM)Tumppi Wrote:
(Jun-23-2017, 08:19 PM)Ofnuts Wrote: This is the purpose of "functions"

Note that you are opening/closing all the files each time, if they are big the performance is going to be awful. You can open the target file once for all before calling the functions (replace parameter with its name with a parameter that hold the open file object). There is likely a simpler logic (fixed intervals) that allows you top open the two source files once for all, especially if they are always read/copied in the forward direction. In which case your code would just be a short loop.

Files are small, how it possible to repeat those lines where "w" and "d" values are with fixed values?

Look my last post, there are same cycle every 4 blocks after #4 command line...

You can create tuples that indicate what the ranges of lines are, and then iterate the tuples:

def copyFileLines(fromFileName,toFileName,start,end):
   linesToCopy=range(start,end)
   with open(fromFileName) as fromFile, open(toFileName, 'a') as toFile:
       for i, line in enumerate(fromFile, 1):
           if i in linesToCopy: 
               toFile.write(line)
    
# use it:
fileName1='file1.txt'
fileName2='file2.txt'
fileName3='file3.txt'
lineRanges=(16,30,20,20),(36,52,42,42),(58,72,62,62),(78,94,84,84),(98,114,104,104)

for start1,stop1,start2,stop2 in lineRanges: 
   copyFileLines(fileName1,fileName3, w+start1,d+stop1)
   copyFileLines(fileName2,fileName3, w+start2,d+stop2)
The lineRanges tuples can be values where d and w  have already been added. And if there is some "logic" in the values you can compute them instead of entering them all by hand.
Unless noted otherwise, code in my posts should be understood as "coding suggestions", and its use may require more neurones than the two necessary for Ctrl-C/Ctrl-V.
Your one-stop place for all your GIMP needs: gimp-forum.net
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 3,484 Oct-17-2023, 06:03 PM
Last Post: Devan
  Need to replace a string with a file (HTML file) tester_V 1 778 Aug-30-2023, 03:42 AM
Last Post: Larz60+
  Replace columns indexes reading a XSLX file Larry1888 2 998 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Delete multiple lines from txt file Lky 6 2,320 Jul-10-2022, 12:09 PM
Last Post: jefsummers
  How to do a mass replace within a CSV file? cubangt 9 5,413 May-09-2022, 06:52 PM
Last Post: snippsat
  I get an FileNotFouerror while try to open(file,"rt"). My goal is to replace str decoded 1 1,414 May-06-2022, 01:44 PM
Last Post: Larz60+
  failing to print not matched lines from second file tester_V 14 6,124 Apr-05-2022, 11:56 AM
Last Post: codinglearner
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,052 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Importing a function from another file runs the old lines also dedesssse 6 2,577 Jul-06-2021, 07:04 PM
Last Post: deanhystad
  [Solved] Trying to read specific lines from a file Laplace12 7 3,570 Jun-21-2021, 11:15 AM
Last Post: Laplace12

Forum Jump:

User Panel Messages

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