mercredi 12 octobre 2016

Python searching through columns

I have a CSV file that I need to loop through in a specific pattern for specific columns and have the output patterns be stored in new files with the same name + "_pattern" + [1,2,3,etc.] + .csv.

This is the search pattern: Loop through column 1 and find the same # and grab them, then loop through column 2 of the grabbed list and then grab all that have the same date in column 2, then go to column 4 and grab all #s that are NOT the same, and then create a file with the pattern from column 1 and 2 and 4 organized by column time.

Example: 1 2 time 4 13.45 9/29/2016 6:00 98765 12.56 9/29/2016 6:05 76548 13.45 9/29/2016 6:07 98764 13.45 9/29/2016 6:21 98766 13.45 9/29/2016 6:20 96765 12.56 9/29/2016 6:06 76553

better view of table Result would be - file_pattern_1.csv would have: 1. 13.45 9/29/2016 6:00 98765 2. 13.45 9/29/2016 6:07 98764 3. 13.45 9/29/2016 6:21 98766 - but would not include: 4. 13.45 9/29/2016 6:20 96765 - because column 4 repeats from a previous entry.

  • file_pattern_2.csv would have:
    1. 12.56 9/29/2016 6:05 76548
    2. 12.56 9/29/2016 6:06 76553

This is what I have so far but I have become lost on the looping logic:

import os

infile = raw_input("Which file are we working with? ")
assert os.path.exists(infile), "Path is incorrect."
os.chdir(infile)

def createFile(csvFile, fileName):
    with open (fileName, 'wb') as ftext:
        ftext.write(csvFile)

def appendFile(csvFile, fileName):
    with open (fileName, 'a') as ftext:
        ftext.write(csvFile)

def setfilename(tread):
    fileName = tread[0:tread.index('.')] + '_patterns' + str(countItem) + '.csv'
    return fileName

for i in pcolumn:
    if pcolumn == pcolumn:
        return pfile
    for x in date:
        if date == date:
            return date
            for a in acolumn:
                if acolumn != acolumn:
                    createFile(fileName)


else:
    print "Finished."

Thank you.

Aucun commentaire:

Enregistrer un commentaire