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
#1
I am a beginner to do any coding, but still i'll try to do something..  Smile

My problem:

i need copy some lines Start.txt to end.txt, only what i know is line numbers, marks/numbers inside the line doest not same all the time, line numbers are. So i cant use .replace method?

Example

Start.txt
       1111111111
       2222222222
       3333333333
       4444444444  <------ This line
       5555555555  <------ And this line
End.txt
       888888888
       999999999
       AAAAAAAAA
       BBBBBBBBB
                 <----- to here
                 <----- to here
i was find some enumerate example before, and try to adjust it, but too hard from beginner.
Something like this what i find:

       with open('start.txt') as fin, open('end.txt', 'w') as fout:
           for i, item in enumerate(fin, 1):
               if i == 7: 
                   item = "string\n" 
               fout.write(item)
This code add only "string" word to line which number i gave...


Thanks!
Reply
#2
If you want to take some line in file1 and write them at the end of file2, it is more like:

linesToCopy=[4,5]
with open('file1.txt') as f1, open('file2.txt', 'a') as f2: # Note "append mode" for target file
    for i, line in enumerate(f1, 1):
        if i in linesToCopy: 
            f2.write(line)
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
#3
(Jun-21-2017, 10:45 PM)Ofnuts Wrote: If you want to take some line in file1 and write them at the end of file2, it is more like:

Yes i want write them file2, but not the end always, some times need write it line 3 or line 10 etc... How i can adjust this which(number) line i'll write?
Reply
#4
Appending will always happen at the end. If you want to mix in between, you would rather use both file1 and file2 as input files, and create a new file3 from the appropiate lines from both files. In the end, you may even rename file3 to file2, if you don't need that source anymore.
Reply
#5
I think its hard to do because there is many "blocks" at the text files where I need copy and paste between the lines?
If here is not any easier method, i Will try this. Can you give example how i read two input files and write one?

No need renaming...

Thanks
Reply
#6
(Jun-22-2017, 06:04 PM)Tumppi Wrote: there is many "blocks" at the text files where I need copy and paste between the lines?
so far it's not clear how you determine between which lines of file2 you should place respective lines from file 1
Reply
#7
I know line numbers from file2 where I want to put it, but there is no easier method than this "multi input" way? Did i understand right?
Reply
#8
(Jun-22-2017, 06:04 PM)Tumppi Wrote: there is many "blocks" at the text files where I need copy and paste between the lines?
If here is not any easier method, i Will try this. Can you give example how i read two input files and write one?

How would you do it with paper & pencil? Think how to go forward step by step, how you can decide which line to look at next, which line to copy or not copy. As of now, I don't understand exactly what you want python to do, and it won't either. You need to be much more specific. Maybe make a small but complete example like you did above but with the new information that file2 can be longer as well, what would be the desired output, and how do you get there?
Reply
#9
(Jun-22-2017, 08:28 PM)Kebap Wrote:
(Jun-22-2017, 06:04 PM)Tumppi Wrote: there is many "blocks" at the text files where I need copy and paste between the lines?
If here is not any easier method, i Will try this. Can you give example how i read two input files and write one?

How would you do it with paper & pencil? Think how to go forward step by step, how you can decide which line to look at next, which line to copy or not copy. As of now, I don't understand exactly what you want python to do, and it won't either. You need to be much more specific. Maybe make a small but complete example like you did above but with the new information that file2 can be longer as well, what would be the desired output, and how do you get there?

Yes i understant, probably I need to do Copy/paste over 100 python strings where I change line numbers every strings.

I can do it still because seems it probably work.

I was try this code:

 linesToCopy=[4,5]
with open('file1.txt') as f1, open('file2.txt', 'a') as f2: # Note "append mode" for target file
    for i, line in enumerate(f1, 1):
        if i in linesToCopy: 
            f2.write(line) 
I think i can do what i want, but linestocopy=[4,5] does not work like i want.

I try linestocopy=[1:65] but ":" mark gives some error.

This means I was supposed to copy lines 1-65... It work if i write "linestocopy=[1,2,3,4,5,6,7,8........]" but i think here is easier way?
Reply
#10
(Jun-23-2017, 06:36 AM)Tumppi Wrote: It work if i write "linestocopy=[1,2,3,4,5,6,7,8........]" but i think here is easier way?

Instead of writing all the numbers separately, you can write range(65) and python will fill it our for you
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Replace a text/word in docx file using Python Devan 4 3,482 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 997 Nov-18-2022, 10:16 PM
Last Post: Pedroski55
  Delete multiple lines from txt file Lky 6 2,318 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,123 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,569 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