Similar type issue from last weeks problem 3 File protocols now

Protocol1 = 7Col x 3Row --> Alt1 Protocol2 = 2Col x 9Row --> Alt2 Protocol3 = 4Col x 7Row --> Alt3

File rows might not be in the same order, need to retain the order from the original to the destination

Output files naming schema - Dest_DTK_alt<#>.csv

Pseudo Code - Identify file type write out appropriate file in the correct order


In [95]:
import csv
import sys
import pandas as pd 

if sys.argv[1] == None:
    print("Export and run this: python3 <filename>.py <file to process>")
    quit()

# Need to open file and peek at structure to make decision
argfile = sys.argv[1]

#debug code
#argfile = 'source.csv'
#argfile = 'alt2.csv'
#argfile = 'alt3.csv'

with open(argfile, 'r') as infile:
    df = pd.read_csv(infile, sep=None)    
    # extract number of cols
    #print(df)
    colNum = len(df.columns)
    if colNum == 2:
        print('logic for fixing the alt2 type')
        # read data from each row, if present, replace data in second column
        # if data is not present in first col then read second col and replace col 1
                
        # dictionary of all possible outcomes
        type1 = ['1', '3']
        type2 = ['2', '4']
        type3 = ['3', '6']
        
        for i, row in df.iterrows():
            if row['ONE'] == 1 or row['TWO'] == 3:
                df.loc[i] = type1
            elif row['ONE'] == 2 or row['TWO'] == 4:
                df.loc[i] = type2
            elif row['ONE'] == 3 or row['TWO'] == 6:
                df.loc[i] = type3                
            else:
                # What have you given me?
                print("The world is on FIRE!")

            df.to_csv('Dest_DTK_alt2.csv', sep='\t', index = False)
        
        
    elif colNum == 4:
        #print('logic for fixing the source.csv')
        data = ['a', 'b', 'c', 'd']
        rowdata=[data]*7
        df = pd.DataFrame(rowdata, columns=['One', 'Two', 'Three', 'Four'])
        df.to_csv(r'Dest_DTK_source.csv', sep='\t', index = False)
    elif colNum == 7:
        #print('logic for fixing the alt3 type')
        data = ['1', '2', '3', '4', '5', '6', '7']
        rowdata=[data]*3
        df = pd.DataFrame(rowdata, columns=['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven'])
        df.to_csv(r'Dest_DTK_alt3', sep='\t', index = False)
    else:
        print("System Error Deleting File System.....Standby...")
        quit()


/Users/xianarchangel/anaconda/lib/python3.6/site-packages/ipykernel/__main__.py:18: ParserWarning: Falling back to the 'python' engine because the 'c' engine does not support sep=None with delim_whitespace=False; you can avoid this warning by specifying engine='python'.

In [ ]:


In [ ]: