| 1 |
"""
|
| 2 |
|
| 3 |
VW_FlattenCsvHoldReport
|
| 4 |
|
| 5 |
Created by:
|
| 6 |
Emanuel Borges
|
| 7 |
12.06.2024
|
| 8 |
|
| 9 |
This simple program will take a VW hold report and flatten two fields, resulting in a report with many more rows.
|
| 10 |
"""
|
| 11 |
|
| 12 |
|
| 13 |
import csv, os
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
if __name__ == '__main__':
|
| 22 |
## Full path and file name for the file to be converted.
|
| 23 |
csvHoldReport = r"C:\Users\eborges\OneDrive - Redgrave LLP\Documents\Cases\VW\2024-12-06 - CodingRequest\2024_10_1_15.21_eDCHold-All_StatusReport_CaseHoldsReport.csv"
|
| 24 |
|
| 25 |
columnNamesToFlatten = ['Exchange locations','SharePoint locations']
|
| 26 |
fileEncoding = 'UTF-16'
|
| 27 |
csvDialect = 'excel'
|
| 28 |
|
| 29 |
|
| 30 |
outputFilePath, outputFileName = os.path.split(csvHoldReport)
|
| 31 |
outputFileName = f"{os.path.splitext(outputFileName)[0]}_CONVERTED{os.path.splitext(outputFileName)[-1]}"
|
| 32 |
outputFileName = os.path.join(outputFilePath, outputFileName)
|
| 33 |
#print(outputFileName)
|
| 34 |
outputFile = open(outputFileName,'w')
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
with open(csvHoldReport,mode='r',newline='',encoding=fileEncoding) as csv_file:
|
| 39 |
csv_reader = csv.DictReader(csv_file, dialect=csvDialect)
|
| 40 |
## Get the column headers
|
| 41 |
fullHeaderList = csv_reader.fieldnames
|
| 42 |
## Create a trimmed version that removes the fields we are going to flatten
|
| 43 |
trimmedHeaderList = fullHeaderList.copy()
|
| 44 |
for colName in columnNamesToFlatten:
|
| 45 |
trimmedHeaderList.remove(colName)
|
| 46 |
## itterate through the rows and flatten the selected fields, adding a full row for each flattened value.
|
| 47 |
for row in csv_reader:
|
| 48 |
for colName in columnNamesToFlatten:
|
| 49 |
flattenedValues = row[colName]
|
| 50 |
flattenedValues = flattenedValues.split(";")
|
| 51 |
for flattenedValue in flattenedValues:
|
| 52 |
pass
|
| 53 |
|
| 54 |
#print(row['Hold name'])
|
| 55 |
print(fullHeaderList)
|
| 56 |
print(trimmedHeaderList)
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
outputFile.close() |