Python Forum
Replace lines from one .txt file to another - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Replace lines from one .txt file to another (/thread-3760.html)

Pages: 1 2


Replace lines from one .txt file to another - Tumppi - Jun-21-2017

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!


RE: Replace lines from one .txt file to another - Ofnuts - Jun-21-2017

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)



RE: Replace lines from one .txt file to another - Tumppi - Jun-22-2017

(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?


RE: Replace lines from one .txt file to another - Kebap - Jun-22-2017

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.


RE: Replace lines from one .txt file to another - Tumppi - Jun-22-2017

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


RE: Replace lines from one .txt file to another - buran - Jun-22-2017

(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


RE: Replace lines from one .txt file to another - Tumppi - Jun-22-2017

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?


RE: Replace lines from one .txt file to another - Kebap - Jun-22-2017

(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?


RE: Replace lines from one .txt file to another - Tumppi - Jun-23-2017

(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?


RE: Replace lines from one .txt file to another - Kebap - Jun-23-2017

(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