Can Cicode read data from a CSV file ?

Can Cicode read data from a CSV file ? if so has anyone done it and are willing to share some sample code.

ideally it would be better if the file was .dbf

Parents
  • There are several things to consider when reading from a CSV. The simplest is if every line is less than 255 characters so it can be read as one string. Then you just need to search for commas in that string to find each field.

    If the lines may be longer than 255 characters, then you need to read multiple blocks of 255 characters, searching each time for commas. If a block has a comma near the end then you need to add the remaining text to the first field of the next block.

    If the CSV has text (not just numbers) then it is common for the text to have quotes around it. That makes your searches more complicated since you have to ignore commas inside of a text string.

    Here's a Cicode function that makes it easier to get the fields from a CSV line. It does not handle quotes, though.

    //Get a field value (text) from a string, where the string consists of a number of fields separated by 
    //a field separation character such as a comma
    //
    //Example:
    //  sText = "ab?cde?fghi?j";
    //  sField = StrGetField(sText,3,"?");
    //
    //In this case sField = "fghi"			
    //
    //Field numbers start at 1.
    //
    STRING FUNCTION StrGetField(STRING sText, INT iField, STRING sFieldSeparator = ",")
    
    	INT	iOffset		= 0;
    	INT	iPos		= 0;
    	INT	iIndex		= 1;
    
    	ErrSet(1);
    	
    	WHILE (sText <> "") AND (iIndex <= iField) DO
    
    		iPos = StrSearch(iOffset, sText, sFieldSeparator);
    		IF (iPos < 0) THEN 
    		
    			iPos = StrLength(sText); 
    		END
    
    		IF (iField = iIndex) THEN
    
    			IF iPos > iOffset THEN
    				RETURN(StrMid(sText, iOffset, iPos-iOffset));
    			ELSE
    				RETURN "";
    			END
    		ELSE
    			iIndex = iIndex + 1;
    			iOffset = iPos + 1 ;
    		END
    	END
    
    	ErrSet(0);
    	
    	RETURN("");
    END
    

    You could also check out the Trend Import CSV tool in the Toolbox. It reads in a CSV file for trend data. The GetLine and Split functions process each line of the CSV. It demonstrates how to handle lines longer than 255 chars. It also uses the StrSearch and StrMid functions directly, instead of using StrGetField() for better performance.

Reply
  • There are several things to consider when reading from a CSV. The simplest is if every line is less than 255 characters so it can be read as one string. Then you just need to search for commas in that string to find each field.

    If the lines may be longer than 255 characters, then you need to read multiple blocks of 255 characters, searching each time for commas. If a block has a comma near the end then you need to add the remaining text to the first field of the next block.

    If the CSV has text (not just numbers) then it is common for the text to have quotes around it. That makes your searches more complicated since you have to ignore commas inside of a text string.

    Here's a Cicode function that makes it easier to get the fields from a CSV line. It does not handle quotes, though.

    //Get a field value (text) from a string, where the string consists of a number of fields separated by 
    //a field separation character such as a comma
    //
    //Example:
    //  sText = "ab?cde?fghi?j";
    //  sField = StrGetField(sText,3,"?");
    //
    //In this case sField = "fghi"			
    //
    //Field numbers start at 1.
    //
    STRING FUNCTION StrGetField(STRING sText, INT iField, STRING sFieldSeparator = ",")
    
    	INT	iOffset		= 0;
    	INT	iPos		= 0;
    	INT	iIndex		= 1;
    
    	ErrSet(1);
    	
    	WHILE (sText <> "") AND (iIndex <= iField) DO
    
    		iPos = StrSearch(iOffset, sText, sFieldSeparator);
    		IF (iPos < 0) THEN 
    		
    			iPos = StrLength(sText); 
    		END
    
    		IF (iField = iIndex) THEN
    
    			IF iPos > iOffset THEN
    				RETURN(StrMid(sText, iOffset, iPos-iOffset));
    			ELSE
    				RETURN "";
    			END
    		ELSE
    			iIndex = iIndex + 1;
    			iOffset = iPos + 1 ;
    		END
    	END
    
    	ErrSet(0);
    	
    	RETURN("");
    END
    

    You could also check out the Trend Import CSV tool in the Toolbox. It reads in a CSV file for trend data. The GetLine and Split functions process each line of the CSV. It demonstrates how to handle lines longer than 255 chars. It also uses the StrSearch and StrMid functions directly, instead of using StrGetField() for better performance.

Children
No Data