I am trying to figure out how to get python to go through a directory full of csv files, process each of the files and spit out a text file with a trimmed list of values.
In this example, I iterate through a CSV with many different types of columns, but all I really want is a first name, last name, and keyword. I have a folder full of these csvs with different columns (except that they all share the first name, last name and keyword somewhere in csv). What is the best way to open this folder, go through each csv file, and then spit it all out as your own csv file for a text list only, as in the example below.
import csv reader = csv.reader(open("keywords.csv")) rownum = 0 headnum = 0 F = open('compiled.txt','w') for row in reader: if rownum == 0: header = row; for col in row: if header[headnum]=='Keyword': keywordnum=headnum; elif header[headnum]=='First Name': firstnamenum=headnum; elif header[headnum]=='Last Name': lastnamenum=headnum; headnum +=1 else: currentrow=row print(currentrow[keywordnum] + '\n' + currentrow[firstnamenum] + '\n' + currentrow[lastnamenum]) F.write(currentrow[keywordnum] + '\n') rownum +=1
source share