Cleaning one CSV file by hand is fine. Cleaning fifty of them, every month, with the same steps, is where mistakes creep in and afternoons disappear. This guide shows how to clean a whole folder of CSV files in one run with Rons Data Stream, and how to convert them to JSON (or XML, HTML and other formats) at the same time.
No code is needed, and the files are processed on your own computer.
What You Will Build
In Rons Data Stream, two things work together:
- A Cleaner says what happens to the data: the clean-up rules.
- A Job says to what: which files to read, which Cleaners to apply, and which format and folder to write to.
Both are saved as files, so next month you only open the Job and click Process Job.
For this example we take a folder of CSV exports and:
- trim extra spaces in every cell
- bring one column to a consistent case
- rewrite dates in one format
- delete a column we don't need
- write each file as clean CSV and as JSON into an output folder
Step 1 – Create a Job and Point It at Your Folder
- Open Rons Data Stream and click New Job on the Start Page.
- Save the Job straight away, next to your data folder. Paths in the Job can then be relative to the Job file, so the Job and its data can be moved together.
- In the Job Palette, under Source Containers, click Directory, then choose the folder that holds your CSV files.
-
Under Source Profiles, click CSV. In its
settings, choose the Source Container, keep the filter
*.csv(or narrow it, for examplesales_*.csv), and set the delimiter if it is not a comma.
Refresh the Job and the matching files appear in the Job output area. Every one of them will be processed in the same run.
Step 2 – Build the Cleaner
Under Data Cleaners, click Embedded Cleaner and then Edit. (If you already have a Cleaner file saved in Rons Data Stream, add it as an External (File) Cleaner instead. Cleaner files saved in Rons Data Edit cannot be used directly.)
A Cleaner needs at least one Column Selector and one Row Selector. By default it has All Columns, Header Row and All Rows. Untick Header Row for rules that should not touch your column names.
Now add the rules from the Cleaner Palette:
| What you want | Rule to add |
|---|---|
| Remove extra spaces | Cell operation Remove Space |
| Consistent case (for example, upper-case province or country codes) | A Column List selector for that column, then cell operation Change Case |
| One date format |
A Column List selector for the date column, then cell operation
Replace Date Time, with the format you want (for example yyyy-MM-dd)
|
| Drop a column | Column operation Delete Column |
Other useful rules for batch clean-ups are Replace Text, Replace Lookup and Fill Lookup (replace or fill values from a list), Split Column and Merge Column, and Delete Row with a Filter or Empty/Filled Rows selector to remove rows you don't want.
The rules run in a set order: header cell rules first, then column operations, then row operations, then the other cell rules. Each rule is numbered, and a rule that still needs settings shows "This rule cannot execute with the current settings".
Step 3 – Choose the Output: Batch Convert CSV to JSON
- Under Output Containers, click Directory and choose an output folder. Use a different folder from the source, so your original files are never overwritten.
- Under Output Formats, click CSV to write clean CSV files, and choose the output folder.
- Click JSON as well, and choose the same (or another) output folder.
Each source file is now written twice: once as clean CSV and once as JSON. The same works for XML, HTML, token text and text templates. To load the data into a database instead, see How to Load CSV Files into SQL Server or SQLite.
Only converting? You don't need a Cleaner at all. A Job with a source, an output format and an output container is a batch file converter.
Optional extras:
- Output Shapers split each output file as it is written: every so many rows (Split - Row Count), by a ratio of rows (Split - Row Ratio), into matching and non-matching rows (Split - Row Filter), or one file per value in a column (Split - Column Value).
- Output Loggers write a log file of the run, which is useful when you process many files.
Step 4 – Check in the Data Builder, Then Process
Open the Data Builder from the top-right corner. Each queued file has its own view showing the original data, the result, and Cell Change, which highlights what each numbered rule changed. If something is wrong, open the Cleaner, adjust the rule, and click Refresh in the Data Builder.
When the result looks right, click Process Job. A progress panel shows each task as processing, complete, pending or with errors. Reopen it any time with View Tasks in the bottom-right corner.
Next Time
Save the Job and the Cleaner. When the next batch of files arrives, drop them in the source folder, open the Job and click Process Job. To run it without opening the application, for example every night, see How to Run a Nightly CSV Clean-up from the Command Line.
Lite or Pro?
Rons Data Stream Lite is free with no time limit and has every option, but processes up to 3 files per run, which is enough to build and test your Job. Rons Data Stream Pro removes the file limit and the advertising banner; it is a one-time purchase of about US$30, not a subscription. Download Rons Data Stream.