Rename Fields Containing Spaces and Symbols

Updated: Dec 11, 2023

This example is designed to rename fields in a dataset, allowing users to modify the names of specific columns or attributes. The Rename a Field example can throw exceptions when renaming columns that contain symbols and spaces since some symbols will be interpreted as expressions (see the Field Path Expressions guide for more on this). This example shows an alternative way on how to do transformations in cases where symbols should not be treated as expressions.

 

Input CSV file

Country (en),Country (de),Country (local),Country code,Continent,Capital,Population,Area,Coastline,Government form,Currency,Currency code,Dialing prefix,Birthrate,Deathrate,Life expectancy,Url
American Samoa,Amerikanisch Samoa,Amerika Samoa,AS,Oceania,,54194,199,116,Presidential democracy (self-governing territory of the US),Dollar,USD,1-684,22.9,4.8,75.4,https://www.laenderdaten.info/Ozeanien/Amerikanisch-Samoa/index.php
British Indian Ocean Territory,Britisches Territorium im Indischen Ozean,British Indian Ocean Territory,IO,Africa,,0,54400,698,British overseas territory,Dollar,USD,246,0,0,0,https://www.laenderdaten.info/Afrika/Britisches-Territorium-im-Indischen-Ozean/index.php
...

 

Java Code Listing

package com.northconcepts.datapipeline.examples.cookbook;

import com.northconcepts.datapipeline.core.DataReader;
import com.northconcepts.datapipeline.core.DataWriter;
import com.northconcepts.datapipeline.core.FieldPath;
import com.northconcepts.datapipeline.core.StreamWriter;
import com.northconcepts.datapipeline.csv.CSVReader;
import com.northconcepts.datapipeline.job.Job;
import com.northconcepts.datapipeline.transform.FastRenameField;
import com.northconcepts.datapipeline.transform.RenameField;
import com.northconcepts.datapipeline.transform.TransformingReader;

import java.io.File;

public class RenameFieldsContainingSpacesAndSymbols {
    public static void main(String[] args) {
        DataReader reader = new CSVReader(new File("example/data/input/countries-1.csv"))
                .setFieldNamesInFirstRow(true);
        reader = new TransformingReader(reader)
                .add(new RenameField(FieldPath.fromName("Country (en)"), "Country"))
                .add(new RenameField(FieldPath.fromName("Currency code"), "Currency Abbr."))
                .add(new FastRenameField("Country (local)", "Country Local Name"))
                .add(new FastRenameField("Government form", "Government Type"));

        DataWriter writer = StreamWriter.newSystemOutWriter();

        Job.run(reader, writer);
    }
}

 

Code walkthrough

  1. CSVReader is created corresponding to the input file countries-1.csv.
  2. The CSVReader.setFieldNamesInFirstRow(true) method is invoked to specify that the names specified in the first row should be used as field names.
  3. A TransformingReader is created to apply one or more transformations to the incoming data sequentially.
  4. RenameField object accepts oldName and newName String arguments. For oldName argument, FieldPath.fromName() method is called to include names that can contain special symbols.
  5. FastRenameField object is another way to rename column names. Here we don't need to call another method to parse an old name.
  6.  Data is transferred from the reader to the StreamWriter(System.out) via Job.run() method.

 

Console Output

-----------------------------------------------
0 - Record {
    0:[Country]:STRING=[American Samoa]:String
    1:[Country (de)]:STRING=[Amerikanisch Samoa]:String
    2:[Country Local Name]:STRING=[Amerika Samoa]:String
    3:[Country code]:STRING=[AS]:String
    4:[Continent]:STRING=[Oceania]:String
    5:[Capital]:STRING=[null]
    6:[Population]:STRING=[54194]:String
    7:[Area]:STRING=[199]:String
    8:[Coastline]:STRING=[116]:String
    9:[Government Type]:STRING=[Presidential democracy (self-governing territory of the US)]:String
    10:[Currency]:STRING=[Dollar]:String
    11:[Currency Abbr.]:STRING=[USD]:String
    12:[Dialing prefix]:STRING=[1-684]:String
    13:[Birthrate]:STRING=[22.9]:String
    14:[Deathrate]:STRING=[4.8]:String
    15:[Life expectancy]:STRING=[75.4]:String
    16:[Url]:STRING=[https://www.laenderdaten.info/Ozeanien/Amerikanisch-Samoa/index.php]:String
}

-----------------------------------------------
1 - Record {
    0:[Country]:STRING=[British Indian Ocean Territory]:String
    1:[Country (de)]:STRING=[Britisches Territorium im Indischen Ozean]:String
    2:[Country Local Name]:STRING=[British Indian Ocean Territory]:String
    3:[Country code]:STRING=[IO]:String
    4:[Continent]:STRING=[Africa]:String
    5:[Capital]:STRING=[null]
    6:[Population]:STRING=[0]:String
    7:[Area]:STRING=[54400]:String
    8:[Coastline]:STRING=[698]:String
    9:[Government Type]:STRING=[British overseas territory]:String
    10:[Currency]:STRING=[Dollar]:String
    11:[Currency Abbr.]:STRING=[USD]:String
    12:[Dialing prefix]:STRING=[246]:String
    13:[Birthrate]:STRING=[0]:String
    14:[Deathrate]:STRING=[0]:String
    15:[Life expectancy]:STRING=[0]:String
    16:[Url]:STRING=[https://www.laenderdaten.info/Afrika/Britisches-Territorium-im-Indischen-Ozean/index.php]:String
}
...
Mobile Analytics