In [4]:
import csv

In [1]:
#Taking the name of the file as an input
fileName = input('Give the name of your file')
print('', fileName)


Give the name of your filesource.csv
 source.csv

In [9]:
#Deleting the contents of the output file

filename = "Output_SRM.csv"
# opening the file with w+ mode truncates the file
f = open(filename, "w+")
f.close()

In [10]:
#Capturing the header row

data = [] #Variable to capture the data of the header row

with open(fileName, 'r') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        data = row

In [11]:
#Based on the delimiter of the data, populating the data in the output file

if ":" in data[0]: 
    # Do the reading
    file1 = open(fileName, 'r')
    #reader = csv.reader(file1)
    reader = csv.reader(file1, delimiter=':', quotechar='|')
    new_rows_list = []
    for row in reader:
        if (row[0] == '1') or (row[1] == '3'):
            new_row = ['1','3']
            new_rows_list.append(new_row)
        elif (row[0] == '2')or (row[1] == '4'):
            new_row = ['2','4']
            new_rows_list.append(new_row)
        elif (row[0] == '3') or (row[1] == '6'):
            new_row = ['3','6']
            new_rows_list.append(new_row)
        else:
            new_row = row
            new_rows_list.append(new_row)

    file1.close()   # <---IMPORTANT
    
    # Do the writing
    file2 = open("Output_SRM.csv", 'w')
    writer = csv.writer(file2)
    writer.writerows(new_rows_list)
    file2.close()
    
elif "," in data[0]:
    file1 = open(fileName, 'r')
    reader = csv.reader(file1, delimiter=',')
    columnCount = len(next(reader))
    
    print(columnCount)
    
    if columnCount == 4:
        with open('Output_SRM.csv', 'w', newline='') as csvfile:
            spamwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
            row = ['One \t' + 'Two\t' + 'Three\t' + 'Four\t']
            spamwriter.writerow(row)
            for i in range(7):
                row = ['1 \t' + '2\t' + '3\t' + '4\t']
                spamwriter.writerow(row)
    elif columnCount == 7 :
        #import csv
        with open('Output_SRM.csv', 'w', newline='') as csvfile:
            spamwriter = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
            row = ['One \t' + 'Two\t' + 'Three\t' + 'Four\t'+ 'Five\t'+ 'Six\t'+ 'Seven\t']
            spamwriter.writerow(row)
            for i in range(3):
                row = ['1 \t' + '2\t' + '3\t' + '4\t'+ '5\t' + '6\t' + '7\t']
                spamwriter.writerow(row)
    else:
        print('unknown type')


4

In [12]:
with open('Output_SRM.csv', newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter=' ', quotechar='|')
    for row in spamreader:
        print(', '.join(row))


One 	Two	Three	Four	
1 	2	3	4	
1 	2	3	4	
1 	2	3	4	
1 	2	3	4	
1 	2	3	4	
1 	2	3	4	
1 	2	3	4	

In [ ]:
#Row Count
# with open(fileName) as f:
#     reader = csv.reader(f, delimiter=' ', skipinitialspace=True)
#     firstRow = next(reader)
#     headerString = ''.join(firstRow)
#     delimiterCount = headerString.count(':')
#     row_count = sum(1 for first_row in f)
#     print('', row_count)
#     f.close