T
T
Timebird2016-07-26 13:36:35
Python
Timebird, 2016-07-26 13:36:35

How to shove values ​​in a file into different files?

Hello!
There is a file file1.txt, in which the frequencies are recorded (on each line by frequency). They can be added or removed. And you can count the number of frequencies in the file.
There is also file2.txt. There values ​​from these frequencies mixed up. That is, if, for example, there are 3 frequencies in the file file1.txt, then in the file file2.txt the numbers go as follows:
number from frequency 1
number from frequency 2
number from frequency 3
number from frequency 1
number from frequency 2
...
Suppose that in the file file1.txt there are not three frequencies, but n. How to write an algorithm that shoves numbers from a specific frequency into specific files?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Movchan, 2016-07-26
@Timebird

Something like this:

files = [open('freq{}.txt'.format(i), 'w') for i in range(N)]

f2 = open('file2.txt')
i = 0
for l in f2.radlines():
    files[i].write(l)
    i = (i+1) % N

f2.close()
for f in files:
    f.close()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question