CSV value dropping leading zeros

Originally posted by GMcCartney0607 on July 24, 2025

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

Originally posted by scot1967 on July 25, 2025

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) :wink:

Originally posted by scot1967 on July 25, 2025

… 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! :+1: Be sure to mark or post the solution to help others out and/or to credit the one who helped you. :smiling_face_with_sunglasses:

Originally posted by rraghvani on July 25, 2025

Ensure your parameters are set up correctly,

This will the correctly produce the following output,

Originally posted by scot1967 on July 25, 2025

…Try forcing everything to be a string.

Originally posted by scot1967 on July 25, 2025

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)).

Originally posted by sean4311 on July 28, 2025

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:\

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

Originally posted by GMcCartney0607 on July 28, 2025

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?

Originally posted by rraghvani on July 29, 2025

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?

Originally posted by Hassan_Ballan on July 29, 2025

Forcing the “Area” column to stay as text

TestComplete uses the OLE DB Text driver under the hood to read CSVs, and by default that driver “guesses” each column’s data type by scanning the first few rows. If it thinks a column is numeric, it will strip leading zeros. You can override that behavior in schema.ini by explicitly declaring each column as Text.

Update your schema.ini, it should look something like this:

[YourFileName.csv]
Format=CSVDelimited
ColNameHeader=True
MaxScanRows=0

; force all three columns to be treated as text
Col1=ID Text
Col2=Area Text
Col3=Name Text
  • Format=CSVDelimited tells the driver it’s a comma-delimited file.
  • ColNameHeader=True makes the first row your header.
  • MaxScanRows=0 forces the driver to scan all rows (not just the first 8) when inferring types.
  • ColN=… Text explicitly sets that column to text, preserving leading zeros.

:speech_balloon: If a response helped you out, don’t forget to Like it! And if it answered your question, mark it as the solution so others can benefit too.

Originally posted by GMcCartney0607 on July 29, 2025

This looks promising. I will try and report back.

Originally posted by GMcCartney0607 on July 29, 2025

Yes, this is how I define the loop variable.

Originally posted by GMcCartney0607 on July 29, 2025

That didn’t do anything. Do I need to uncheck the box in the definition that says to override defaults?

Originally posted by GMcCartney0607 on July 29, 2025

I can’t do that because it requires additional drivers. I have implemented a workaround to pad the values before using them.

Wasteful use of CPU, but works.

Originally posted by rraghvani on July 29, 2025

The screenshot that I have provided, is the exact settings you require - the preview actually shows ID, Area and Name being read as strings, and maintaining leading zeros. Which can be seen in the output results.

Originally posted by Hassan_Ballan on July 29, 2025

There are two parts to validate: reading the CSV value as text (validated in the Data Driven Loop dialogue as rraghvani​ stated, if it reads it and retain the leading zeros then it is correct), and then using the text value in TestComplete (may need to specify it’s String not Variant as it would convert it automatically).
If you are using https://support.smartbear.com/testcomplete/docs/testing-with/variables/data-types/table.html the default is Variant.

Originally posted by Hassan_Ballan on August 22, 2025

GMcCartney0607​ was the issue resolved?

:speech_balloon: Found the answer helpful? Give it a Kudos by clicking Like!
:white_check_mark: Got your issue resolved? Click Mark as Solution so others can find it quickly.