Changing CSV delimiter and other parameters

By default in the CSV-files a comma is used as a separator for the fields.

In some instances however, it is more convenient to use another separator (for example, a semicolon, if the data often contains text with commas).

In this recipe we will consider changing the separators in the given CSV files.

Getting ready

First of all, let's create a file and a script, which will be working with customizations by default:

  1. Create a file C:data.csv with the following content:
    id,First,Last
    1,John,Doe
    2,Jane,Smith
  2. Now, let's create a script, which will be reading the data from the given file:
    function testCSVDelimiter()
    {
      var data = DDT.CSVDriver("c:\data.csv");
      while(!data.EOF())
      {
        Log.Message(data.Value("First"), data.Value("Last"));
        data.Next();
      }
      DDT.CloseDriver(data.Name);
    }

How to do it...

In order to have TestComplete read out the data that are separated by semicolon, it is necessary to complete the following actions:

  1. Change the file C:data.csv by changing commas to semicolons:
    id;First;Last
    1;John;Doe
    2;Jane;Smith
  2. Create the C:schema.ini file with the following content:
    [data.csv]
    Format=Delimited(;)
  3. Launch the testCSVDelimiter function. In the result, the function will correctly read all the data, pre-separated by semicolon instead of the comma.

How it works...

The file schema.ini is an ordinary INI file, which contains various parameters for CSV files in the same folder, where it is to be found. As a name of the section, the name of the file is used, and then their values are signified also. For example, in our case, we have denoted that data is to be separated by semicolon (the parameter Format=Delimited(;)).

This file can contain settings for several files that are to be found in the same catalogue.

Note

Note that the name of the INI file should be exactly schema.ini.

There's more...

Apart from the data separators, the schema.ini file can contain many more customizations (whether the file contains headings of the columns, the text encoding, data types for each of the columns, and so on). Reading up in greater detail about these settings is possible via the following link: http://msdn.microsoft.com/en-us/library/ms709353.aspx.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
52.15.55.18