Forum Discussion

GMcCartney0607's avatar
GMcCartney0607
Occasional Contributor
2 months ago

CSV value dropping leading zeros

I have created a Keyword test that reads a CSV file for a data-driven loop. The file has string values enclosed in quotes, such as;

ID, Area, Name
"123435","001","John Doe"

When reading the file, the values are stripped of their leading zeros. My application expects the leading zero so I need it to stay. Do I have any options?

Thanks

 

16 Replies

  • scot1967's avatar
    scot1967
    Icon for Champion Level 2 rankChampion Level 2

    Hi GMcCartney0607,

    I have seen this before or something similar.  Which version are you on?  I will have a look through my notes and reply. (Unless someone beats me to it) 😉

  • scot1967's avatar
    scot1967
    Icon for Champion Level 2 rankChampion Level 2

    ... Ok, my first hit is dealing with string conversion to integer at some point.  Could you show us a code snip?   I would expect that 0 in, "0John Doe" would not be removed.

    P.S. I am thinking of the file read process here. Let me know?

    ... If you find my posts helpful drop me a like! 👍 Be sure to mark or post the solution to help others out and/or to credit the one who helped you. 😎

  • rraghvani's avatar
    rraghvani
    Icon for Champion Level 3 rankChampion Level 3

    Ensure your parameters are set up correctly,

    This will the correctly produce the following output,

     

  • scot1967's avatar
    scot1967
    Icon for Champion Level 2 rankChampion Level 2

    This is a link explaining the process of reading data from a file.  This takes to the KWT section and has a few links to more details.

    https://support.smartbear.com/testcomplete/docs/testing-with/data-driven/retrieving-input.html#keyword-test-operations

    Changing the Data Type of the Retrieved Value

    TestComplete uses DDT Drivers to retrieve information from a storage. In most cases, the data is retrieved as a string. However, it can also be retrieved as an integer. Some methods perform different actions depending on whether you use integers or strings. For example, the ClickItem action performs a touch on an item using its index if you use an integer, but if you use a string, it searches for the corresponding item caption.

    If you need to change the data type, use the VarToStr and VarToInt methods to make sure that you use a string or an integer value.

    To use these methods in keyword tests, do the following:

    1. Double-click the Value cell of the Keyword Test operation to open the Parameters Editor.
    2. Click the ellipsis button.
    3. Copy the contents of the Value field to the Clipboard.
    4. Change the Mode option to Code Expression.
    5. In the value column, type in either VarToStr() or VarToInt().
    6. Paste the Clipboard contents inside the parentheses and click OK.

    In scripts, use these methods to change the data type of the value you retrieve, for example, VarToStr(Driver.Value(5)).

  • GMcCartney0607​ You can try to avoid this by building a schema.ini file that sits in the same path as your csv file. Syntax of file is listed below:\

    https://learn.microsoft.com/en-us/sql/odbc/microsoft/schema-ini-file-text-file-driver?view=sql-server-ver17

    Specifics about the ini file are also found here as well:

    https://support.smartbear.com/testcomplete/docs/testing-with/data-driven/csv-storages.html

    • GMcCartney0607's avatar
      GMcCartney0607
      Occasional Contributor

      I have a schema.ini, but it only specifies the delimiter. Are you saying that I need to specify the field type (string) and length so that TC it will not convert and drop the leading zero for the quoted strings?

      • rraghvani's avatar
        rraghvani
        Icon for Champion Level 3 rankChampion Level 3

        You didn't mention that you were using scheme.ini in your original post!?

        Have you used the following dialog to create your Data-Driven Loop?

        If you haven't used the above dialog, what exactly are you using to read your CSV file?