Run more than one csv file using python

Asked

Viewed 39 times

0

I have the following question: I read a csv file and in this file I want to take the lines 0:9 and leave the 10 on. I made this code and everything seems to be ok. My problem is: How to read multiple csv’s files like this and do that same process.

import csv

with open("teste2.csv") as f:
    reader = csv.reader(f)
    data = [r for r in reader]
    header = data[0:9]
    data = data[10:]
    for row in data:
        print(row)

Arquivo csv

1 answer

0

One way to do it is by using the functions listdir and isfile.

Follow an example:

from os import listdir
from os.path import isfile

diretorio = '/home/usuario'
lista = listdir(diretorio)
for f in lista:
    if isfile(f):
        print(f)

With this script you can adapt your need, instead of printing you can run the routine you want.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.