Sidebar

Why is a field in my CSV record quoted when there is no special character?

0 votes
178 views
asked Sep 25, 2019 by ben-s-7515 (11,510 points)
I have the followin CSV file:

Name,Address,City,State,Zip
Ben,123 N 5600 W, Kaysville,UT,84405

When I process this file with QIE, the result is:

Name,Address,City,State,Zip
Ben,123 N 5600 W," Kaysville",UT,84405

Why is the city quoted?  It does not have any special characters.

1 Answer

0 votes

In this case it is caused by the city having a space at the front of the field.  Even though technically the space is allowd in the CSV standard, the libraries that are used place this field in quotes to ensure that the user knows that there is a space leading or trailing the value in the field.  Any standard CSV parser should be able to parse this result without any problems, but if the quotes can not be included in the result, then you would need to trim the spaces off of the message.  This is an example of trimming the "City" value.

for (var i = 1; i <= message.getRowCount(); i++) {
   message.setNode('City[' + i + ']', StringUtils.trim(message.getNode('City[' + i + ']')));
}

Using the above script the result is now:

Name,Address,City,State,Zip
Ben,123 N 5600 W,Kaysville,UT,84405

You will notice that the value Kaysville is no longer lead with a space.

answered Sep 25, 2019 by ben-s-7515 (11,510 points)
...