'Is it possible to read structured data from a CSV in Genexus?

Genexus has the ExcelDocument data type that allows you to read data in a tabular way from an excel file, specifying rows and columns. Is there a way to do the same with a csv file? I can open it and read it like a normal txt, but a structure would be more effective



Solution 1:[1]

Yes, it's posible, using Delimited ASCII files functions

Solution 2:[2]

As ealmeida explained you can use the Delimited ASCII files functions.

Below you can see an example on how to code both read and write operations.

ASCII File sample

1,"Jane Doe",1955-05-21
2,"John Smith",1991-10-15
3,"William Shakespeare",2005-11-30

Setup variables with parameters

&FullFileName = !'Datos.txt'
&RecordLength = 50
&FieldsDelimiter = !','
&StringDelimiter = !'"'
&DateFormat = !'ymd'
&DateSeparator = !'-'

To read Delimited ASCII

&ErrorNbr = DFROpen(&FullFileName, &RecordLength, &FieldsDelimiter, &StringDelimiter)
do while DFRNext() = 0
    &ErrorNbr = DFRGNum(&PersonNumber)
    &ErrorNbr = DFRGTxt(&PersonName)
    &ErrorNbr = DFRGDate(&PersonDOB, &DateFormat, &DateSeparator)
enddo
&ErrorNbr = DFRClose()

To write Delimited ASCII

&ErrorNbr = DFROpen(&FullFileName, &RecordLength, &FieldsDelimiter, &StringDelimiter)
for each Person
    &ErrorNbr = DFWPNum(PersonNumber, 0)
    &ErrorNbr = DFWPTxt(PersonName)
    &ErrorNbr = DFWPDate(PersonDOB, &DateFormat, &DateSeparator)
    &ErrorNbr = DFWNext()
endfor
&ErrorNbr = DFWClose()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ealmeida
Solution 2