Consultor Eletrônico



Kbase P112126: How to import a CSV file which contains commas in the data ?
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   28/10/2010
Status: Unverified

GOAL:

How to import a CSV file which contains commas in the data ?

GOAL:

Importing a CVS file with commas within the data

FIX:


This code only provides a way to separate the entries of the record string ,
additional customization has to be performed to load the data to the indicated records.
This code assumes that the format of the datafile.csv has the following format :
"12345","abcdef","56789","ghi, jklmn","031415"
"12345","abcdef","56789","abc, defg","031415"
"12345","abcdef","56789","zxy, abcd","031415"

Code :
DEF VAR myRow AS CHAR.
DEF VAR i AS INTEGER.
FUNCTION getEntries RETURN CHAR (INPUT theStr AS CHAR):
REPEAT i = 2 to NUM-ENTRIES(theStr, '"') BY 2:
/* Here we get each different entry from the line,
this can be stored in a variable and then assigned to a field */
MESSAGE ENTRY(i,theStr,'"') VIEW-AS ALERT-BOX.
END.
END.
INPUT FROM "datafile.csv".
REPEAT:
IMPORT UNFORMATTED myRow.
getEntries(myRow).
END.

An alternative method using the IMPORT DELIMITER statement would be:

DEFINE VARIABLE myRow AS CHARACTER EXTENT 5 NO-UNDO.
INPUT FROM "datafile.csv".
REPEAT:
IMPORT DELIMITER ',' myRow.
MESSAGE "MyRow : "
myRow[1] skip
myRow[2] skip
myRow[3] skip
myRow[4] skip
myRow[5]
VIEW-AS ALERT-BOX INFO BUTTONS OK.
END.