Create a script that processes a CSV file

This section explains how a CSV file that is uploaded to the storage is read and output to another CSV file after the data is converted.

Description

The sample project reads a CSV file, converts the character string stored in "ProductName", and then outputs it to another CSV file.

Processing flow

Take the following steps in order to create a script that processes a CSV file.

 

Key functions
  • Read CSV File

    Configure the settings to read data from a CSV file.

    You can set them from File > CSV > Read CSV File of the tool palette.

  • Write CSV File

    Configure the settings to write data to a CSV file.

    You can set them from File > CSV > Write CSV File of the tool palette.

  • Replace Each String logic

    This logic replaces the input character string according to a replacement table and returns the value of the string.

    You can set it from String > Conversion > Replace Each String of the tool palette in Mapper.

     

How to use sample data

The sample data to execute the above processing is provided.

Download the sample data from Scenario list and extract it.

Sample data name: tutorial_CSV_script.zip

Procedures

(1) Upload a CSV file to storage

  1. Select > SERVICES > Storage.

  2. On the Storage page, select the folder to upload the CSV file to.

    In this example, select the data folder under Personal.

  3. Select Upload File and then select the CSV file to upload (inputdata.csv).

     

(2) Create a project

  1. Select > HULFT INTEGRATE > Projects.

  2. On the Projects page, select Create Project.

  3. In step General on the Projects > Add New page, set the fields related to the project as shown below, and then select Next.

    Field name

    Setting (example)

    Name

    sample1

    Workspace

    Personal

    Description

    (Optional)

    Create Script

    Select the checkbox.

    Script Name

    script

  4. In step Summary confirm the entered contents and select Finish.

     

(3) Launch Designer

  1. In the Edit sample1 dialog, specify Profile and Integrate service.

    For details about profiles, refer to Profiles.

    For details about Integrate services, refer to HULFT Integrate.

  2. Select Launch Designer.

    Note

    To launch Designer, HULFT Square Designer.exe must be installed.

    = Remarks =

    If you canceled launch of Designer, or when you edit a script in an existing project, select the Open project icon of the project to launch Designer.

  3. Select Open StudioLauncher at the top of the screen.

 

(4) Place an operation that reads a CSV file

  1. Double-click a created script in the project explorer shown on the left of the Designer screen to open the script.

  2. From the tool palette shown on the right of the Designer screen, drag File > CSV > Read CSV File onto the script canvas.

  3. On the New Read CSV File operation screen, configure the settings in order to read a CSV file.

    Field name

    Setting value

    Description

    Name

    csv_read

    Enter a component name (single-byte alphanumeric characters and underscores)

    The default value of csv_read is used in this example.

    File

    /Personal/data/inputdata.csv

    Specify a CSV file to be read

    Select Browse and then select a CSV file from the storage. In this example, select the CSV file that was prepared in the first step, inputdata.csv, and select Open.

    Delimiter mode

    Select from list

     

    Delimiter

    Comma

     

    Column list

    ProductName

    OrderAmount

    Select Add and enter ProductName in the input field of Column name.

    Select Add again and enter OrderAmount in the input field of Column name.

  4. Select Finish.

     

(5) Place an operation that writes a CSV file

  1. From the tool palette, drag File > CSV > Write CSV File onto the script canvas.

  2. On the New Write CSV File operation screen, configure the settings in order to write a CSV file.

    Field name

    Setting value

    Description

    Name

    csv_write

    Enter a component name (single-byte alphanumeric characters and underscores)

    The default value of csv_write is used in this example.

    Input data

    (No input data)

     

    File

    /Personal/data/outputdata.csv

    Specify a CSV file to be written

    In this example, specify /Personal/data/outputdata.csv as the path and the output CSV file name.

    Delimiter mode

    Select from list

     

    Delimiter

    Comma

     

    Column list

    ProductName

    OrderAmount

    Select Add and enter ProductName in the input field of Column name.

    Select Add again and enter OrderAmount in the input field of Column name.

  3. Select Finish.

     

(6) Create a mapping in the mapping canvas

  1. Drag the csv_read component icon onto csv_write.

  2. Select Draw process flow and data flow from the dialog that appears after the icon is dropped.

  3. Select Add mapping, and then select OK.

    = Remarks =

    If you select Cancel in the dialog, you can re-display the dialog by dragging the Read CSV File operation onto the Write CSV File operation again.

  4. Double-click the mapping icon that is created on the script canvas.

  5. In Mapper editor, create processing that converts character string "A" in the input character string to "B".

    Configure the mapping canvas as shown below.

    No.

    Logic name

    Description

    Location on tool palette

    (1)

    Iterate specified node

    This logic iterates the specified node.

    Loop > Basic > Iterate specified node

    (2)

    Replace Each String

    This logic replaces the input character string according to a replacement table and returns the value of the string.

    Enter the following values in Replacement table on the Required settings tab.

    Replace from

    Replace to

    A

    B

    Select Partial match for Matching method.

    Select Partial replacement for Replacing method.

     

    String > Conversion > Replace Each String

    = Remarks =

    Select Partial match for Matching method and Partial replacement for Replacing method in the Replace Each String logic.

    With the above combination, the character strings in the input character string that partially matches the character string specified for Replace from are replaced with the character string of Replace to.

  6. Select Finish to close the mapping canvas and go back to the script canvas.

     

(7) Draw a flow on the script canvas

  1. Drag the Start icon onto csv_read.

    A solid black arrow is displayed from the Start icon to csv_read.

    = Remarks =

    The process flow is displayed with a solid black line. The data flow is displayed with a yellow dashed line.

  2. Drag csv_write onto the End icon.

    A solid black arrow is displayed from csv_write to the End icon.

     

(8) Save the created script

  1. Save the script by selecting Save project from the File menu or with Ctrl+S.

    For details about keyboard shortcuts, refer to Keyboard shortcuts.

    Note

    While the project is being saved, if a problem occurs such as a network disconnection, the project may be saved incorrectly and can't be reopened.

(9) Debug the script

  1. Select Start/Restart debug in the Run menu.

    = Remarks =

    Scripts can also be executed from in the tool bar.

  2. Check the execution log shown on the bottom of the Designer screen, and confirm that the script was debugged.

    = Remarks =

    By running debug, you can view the processing time for each component and view the script execution log.

    The execution history view shows the overall processing time and the processing time for each icon.

(10) Execute the script

  1. Select Run from the Run menu.

    = Remarks =

    Scripts can also be executed from in the tool bar.

  2. In the Run script dialog, select OK.

    = Remarks =

    The execution history view shows the processing time of the script execution.

  3. Check the execution log shown on the bottom of the Designer screen, and confirm that the script was executed.

(11) Exit Designer

  1. Select X on the upper-right of the Designer screen.

  2. Exit HULFT Square Designer? is shown on the screen.

    Select OK.

  3. Go back to the HULFT Square site.

(12) Check that the file is created on the storage

  1. Select > SERVICES > Storage.

  2. Check that "outputdata.csv" is created in the specified folder of the storage.

  3. Open "outputdata.csv" and confirm that the character string is replaced.

    Download the file with Download in the action menu and confirm that the contents of the file have been changed as follows.

    SampleDataB,100 
    SampleDataB,200 
    SampleDataC,300

(13) Confirm the service event log

  1. Select > SERVICE EVENTS > HULFT Integrate.

  2. Confirm the log with the project name "sample1" in the HULFT Integrate page.

 

Now, the procedure for processing a CSV file that is uploaded to the storage and outputting it to another CSV file is completed.