Posts: 7
Threads: 1
Joined: Jun 2017
Jun-21-2017, 07:34 PM
(This post was last modified: Jun-22-2017, 08:31 PM by Kebap.)
I am a beginner to do any coding, but still i'll try to do something..
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!
Posts: 687
Threads: 37
Joined: Sep 2016
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
Posts: 7
Threads: 1
Joined: Jun 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?
Posts: 470
Threads: 29
Joined: Sep 2016
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.
Posts: 7
Threads: 1
Joined: Jun 2017
Jun-22-2017, 06:04 PM
(This post was last modified: Jun-22-2017, 08:29 PM by Kebap.)
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
Posts: 8,157
Threads: 160
Joined: Sep 2016
(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
Posts: 7
Threads: 1
Joined: Jun 2017
Jun-22-2017, 08:08 PM
(This post was last modified: Jun-22-2017, 08:29 PM by Kebap.)
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?
Posts: 470
Threads: 29
Joined: Sep 2016
(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?
Posts: 7
Threads: 1
Joined: Jun 2017
Jun-23-2017, 06:36 AM
(This post was last modified: Jun-23-2017, 06:36 AM by Tumppi.)
(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?
Posts: 470
Threads: 29
Joined: Sep 2016
(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
|