Python-dismembering text file

Good Night,

I need to separate a few lines from the file and according to the line append in another file. That is, a file containing 6 words will be added, according to the word for a specific file.

These 6 words can increase to 8, 10, etc and ai will have to create 8, 10 files, and so on.

I first tried to create an array in which each row would be responsible for a row that contain the word.

But I could not, because as much as I tried to add in the row, I had no way, because I could not unless I specified the entry with row and column.

For example, I want all lines containing orange to be directed to the Orange file.txt and that all lines containing plum go to the plum file.txt.

The idea would be to make the code without "if Plum", "if Orange".

I tried throwing the words into a vector but to play in the file I could not without having the if... Ex:

frutas = ['laranja', 'ameixa']

with open('frutas.txt', 'r') as arq_fruta:
  for line in arq_fruta:
    coluna = line.split()
    for i in range (len(frutas)):
      if(coluna[1] == variaveis[0]):
        laranja.append(coluna[0] +' '+ coluna[3]+'\n')

The Last Line I could not put as a vector for example, something like:

fruta[i].append(coluna[0] +' '+ coluna[3]+'\n') #só como exemplo, nao funciona

Being that the fruit [0] would be the vector of all lines containing only orange and fruit[1] all lines with plum.

I tried to create an array, but it did not work, because the array asks the row and column for input, but I do not have these infos, since I will read the and supposedly throw to the file.

E speaking of file, I also tried to do something that was "direct" but also does not work.

for i in range(1, len(frutas)):
   arq = open(frutas[i]+'.txt','w')
   arq.writelines(fruta[i])

Do you have a more "correct" way of doing this? I did not get success, only with the code with " if " which would have a lot of change if I had to include another fruit for example.

Author: Pinheiro, 2018-11-15

1 answers

A more direct way would be to create a list of open files, where you have a file for each fruit. So you can have a single code that writes to all files directly without having to split into lists in memory. The code will be able to handle files of any size because it writes directly to the destination.

frutas = ['laranja', 'ameixa']
arquivos = [open(fruta + '.txt', 'w') for fruta in frutas]

with open('frutas.txt', 'r') as arq:
    for linha in arq:
        for fruta, arquivo in zip(frutas, arquivos):
            if fruta in linha:
                arquivo.write(linha)

If you even want to separate into variables in memory, one solution is to combine dictionaries with lists, it can be facilitated by collections.defaultdict:

import collections

frutas = ['laranja', 'ameixa']
por_fruta = collections.defaultdict(list)

with open('frutas.txt', 'r') as arq:
    for linha in arq:
        for fruta, arquivo in zip(frutas, arquivos):
            if fruta in linha:
                por_fruta[fruta].append(linha)

So you have all the lists in the dictionary por_fruta... to write to file later:

for fruta, linhas in por_fruta.items():
    with open(fruta + '.txt', 'w') as f:
         f.writelines(linhas)
 1
Author: nosklo, 2018-11-16 01:15:32