How to apply recursive action in csv.DictReader

0

Hello, I created the following code but it only applies the function to create the txt file using the last record of the CSV table, how do I get it to create a file for each record of that table?

import csv
import os
with open('lista.csv') as listacsv:
leitor = csv.DictReader(listacsv)
for row in leitor:
    a = row['nome']
    b = row['empresa']

os.mknod("{}_{}.txt".format(a,b))

The sample CSV file follows:

nome,empresa,logo
Diego Rocha,Espaço Rocha,espacorocha.png
Adiana Bonfim,Bonfim Select,bonfim.png
Cris Matarazzo,Spazio Odontologia,spazio.png
    
asked by anonymous 13.10.2017 / 10:47

1 answer

0

You've come close to solving the problem, however, you've been unaware of indentation . In Python this is crucial and delimits the blocks of code.

Here's a solution:

import csv
import os

with open('lista.csv') as listacsv:
    leitor = csv.DictReader(listacsv)
    for row in leitor:
        a = row['nome']
        b = row['empresa']
        nome = "{}_{}.txt".format(a,b).replace(" ","_")
        os.mknod(nome);

Recorded Files:

Adiana_Bonfim_Bonfim_Select.txt
Cris_Matarazzo_Spazio_Odontologia.txt
Diego_Rocha_Espaço_Rocha.txt
    
13.10.2017 / 12:35