Dealing with CSV challenges in Wrangler
This page describes various options available in Wrangler to deal with complex scenarios of processing CSV / TSV or any delimited files.
Before you begin
Some of the instructions here require you to use power-mode in Wrangler. Power-mode in Wrangler exposes the full capabilities of Wrangler directives. Power-mode is available at the bottom of the Wrangler screen.
Non-Printable and CTRL-M Characters
Avoid using automatic header detection with parse-as-csv
directive(parse-as-csv :col ‘\t’ false
). On large files that are distributed across multiple partitions, the header line which is the first line of the CSV file is not present. This will either result in failure or records will be lost.
Sometimes a file looks fine, but it could contain non-printable ASCII characters that usually don’t belong in CSV files. It can be hard to track these down. Use the
find-and-replace
directive.`find-and-replace :col 's/\000-\007\013-\037//g'
.To remove CTRL-M from the end of each line, again use the
find-and-replace
directive.find-and-replace :col 's/\r$//g'
if you want to remove CTRL-M at end of the line. Make sure you apply this directive before applyingparse-as-csv
.
Incorrect Width
Filtering records that do not have a specified number of columns in a record can be achieved with
send-to-error
orfilter-row
. For example,send-to-error exp : { this.width() < 4 }
will send all records that have less than 4 (0..3) to error.filter-row exp: { this.width() < 4 } true
will filter records that are less than 4 from the main dataset.To find rows that have issues, you can use
filter-row
with a minor change as follows:filter-row exp: { this.width() < 4 } false
. This will show you only rows that are problematic.
Â
Created in 2020 by Google Inc.