Updates the delimiter of all CSV files in a directory.
Originally built to solve a specific problem, where a database could only be exported as a comma delimited CSV with columns containing commas surrounded with quotes:
1,Some text,"Sentence, with a comma",More text, -- 4 columns
The issue is that SQL Server BULK INSERT
does not recognise the quotes and in the above example would try to populate 5 columns and error.
-
Place CSVRedelimiter.py in folder with the CSV file(s)
-
Create the output directory (Default: ./Fixed)
-
Run
python3 CSVRedelimiter.py -d "," -r "^" -s "fixed" -k
-
OR - Update variables to meet your needs
initialChar
- Character you want replacedreplaceChar
- Character to replace withremoveQuotes
- True to also remove quotes, False keeps quotessavePath
- The directory to save the updated CSV(s) to. Must exist before runningdateFormat
- The date format for any date values in the csvdateFormatRegex
- The regex equivalent ofdateFormat
used to find all instances of the date to replacesqlDateFormat
- The date format accepted by the database or destinationfixDates
- True to convert any content matchingdateFormat
tosqlDateFormat
-
Run
python CSVRedelimiter.py
Originally developed by @Rollinginsanity, extended by me.