Skip to main content
Title
Link
tripleee
  • 192k
  • 37
  • 319
  • 370

How to return every kth element of outputinput file in inputoutput file?

I have txta text input file:

AAA
BBB
CCC
DDD
EEE
FFF,FFF
GGG

I want to copy every kth line of this file into an output txttext file. forFor example, for k=2 it would be:

BBB
DDD
FFF FFF

andAnd for k = 3 it should be

CCC
FFF FFF

thisThis is what I have done so far, I am struggling to find how to identify K in the function. couldCould you please help me?

def ex1 (filen1, filen2, k):
    fp1=open(filen1,'r') #open file to read from
    fp2=open(filen2,'a') #open file to write to
    
    count=1 #number of lines which have been copied
    
    for line in fp1:
        line = next(str(filen1))
        list.append(line.strip())
        fp1.readline
    return list     
fp1.close
fp2.close    
ex1('Input file.txt', 'Output.txt')

I have txt input file:

AAA
BBB
CCC
DDD
EEE
FFF,FFF
GGG

I want to copy every kth line of this file into output txt file. for example for k=2 it would be:

BBB
DDD
FFF FFF

and for k = 3 it should be

CCC
FFF FFF

this is what I have done so far, I am struggling to find how to identify K in the function. could you please help me?

def ex1 (filen1, filen2, k):
    fp1=open(filen1,'r') #open file to read from
    fp2=open(filen2,'a') #open file to write to
    
    count=1 #number of lines which have been copied
    
    for line in fp1:
        line = next(str(filen1))
        list.append(line.strip())
        fp1.readline
    return list     
fp1.close
fp2.close    
ex1('Input file.txt', 'Output.txt')

I have a text input file:

AAA
BBB
CCC
DDD
EEE
FFF,FFF
GGG

I want to copy every kth line of this file into an output text file. For example, for k=2 it would be:

BBB
DDD
FFF FFF

And for k = 3 it should be

CCC
FFF FFF

This is what I have done so far, I am struggling to find how to identify K in the function. Could you please help me?

def ex1 (filen1, filen2, k):
    fp1=open(filen1,'r') #open file to read from
    fp2=open(filen2,'a') #open file to write to
    
    count=1 #number of lines which have been copied
    
    for line in fp1:
        line = next(str(filen1))
        list.append(line.strip())
        fp1.readline
    return list     
fp1.close
fp2.close    
ex1('Input file.txt', 'Output.txt')
Rollback to Revision 2
Source Link
Anna
  • 25
  • 4

I have txt input file: AAA BBB CCC DDD EEE FFF,FFF GGG

AAA
BBB
CCC
DDD
EEE
FFF,FFF
GGG

I want to copy every kth line of this file into output txt file. for example for k=2 it would be: BBB DDD FFF FFF and

BBB
DDD
FFF FFF

and for k = 3k = 3 it should be CCC FFF FFF

CCC
FFF FFF

this is what I have done so far, I am struggling to find how to identify K in the function. could you please help me?

def ex1 (filen1, filen2, k):

def ex1 (filen1, filen2, k):
    fp1=open(filen1,'r') #open file to read from
    fp2=open(filen2,'a') #open file to write to
    
    count=1 #number of lines which have been copied
    
    for line in fp1:
        line = next(str(filen1))
        list.append(line.strip())
        fp1.readline
    return list     
fp1.close
fp2.close    
ex1('Input file.txt', 'Output.txt')

fp1.close fp2.close
ex1('Input file.txt', 'Output.txt')

I have txt input file: AAA BBB CCC DDD EEE FFF,FFF GGG

I want to copy every kth line of this file into output txt file. for example for k=2 it would be: BBB DDD FFF FFF and for k = 3 it should be CCC FFF FFF

this is what I have done so far, I am struggling to find how to identify K in the function. could you please help me?

def ex1 (filen1, filen2, k):

fp1=open(filen1,'r') #open file to read from
fp2=open(filen2,'a') #open file to write to

count=1 #number of lines which have been copied

for line in fp1:
    line = next(str(filen1))
    list.append(line.strip())
    fp1.readline
return list     

fp1.close fp2.close
ex1('Input file.txt', 'Output.txt')

I have txt input file:

AAA
BBB
CCC
DDD
EEE
FFF,FFF
GGG

I want to copy every kth line of this file into output txt file. for example for k=2 it would be:

BBB
DDD
FFF FFF

and for k = 3 it should be

CCC
FFF FFF

this is what I have done so far, I am struggling to find how to identify K in the function. could you please help me?

def ex1 (filen1, filen2, k):
    fp1=open(filen1,'r') #open file to read from
    fp2=open(filen2,'a') #open file to write to
    
    count=1 #number of lines which have been copied
    
    for line in fp1:
        line = next(str(filen1))
        list.append(line.strip())
        fp1.readline
    return list     
fp1.close
fp2.close    
ex1('Input file.txt', 'Output.txt')
Rollback to Revision 1
Source Link
Anna
  • 25
  • 4
Loading
added 104 characters in body
Source Link
user2390182
  • 73.7k
  • 6
  • 71
  • 95
Loading
Source Link
Anna
  • 25
  • 4
Loading