Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Problem CSV File
Message
From
12/10/2007 20:16:52
Neil Mc Donald
Cencom Systems P/L
The Sun, Australia
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01258459
Message ID:
01260770
Views:
19
Hi Sergey,

Thanks for your prior help with this problem, the solution ended up requiring another approach.

The main problems were commas & CR LF pairs in the character elements as well as CR LF pairs in the numeric elements.

For anyone that has a similar problem I have listed the code below, also any suggestions to improve the code would be welcomed.
Local lcChk, lcFileToImport, lcString, lcValue
LOCAL lnBlockSize, lnCurPos, lnElement, lnNoElements, lnFH, lnFinPos, lnSize

lcFileToImport = Getfile("", "Select File to Import")
If Len(lcFileToImport) = 0
	Return
Endif

lnFH = Fopen(lcFileToImport, 2)
If lnFH < 0
	Messagebox("Cannot open '" + lcFileToImport + "'", 0, "Cannot fix")
	Return
ENDIF

CREATE CURSOR curTable (f1 C(5),f2 M,f3 C(8),f4 C(5),f5 C(3),f6 C(3),f7 C(3),f8 C(5),f9 C(8),f10 C(5),f11 C(8))  

lnBlockSize = 2048 & set read block size

DO WHILE !FEOF(lnFH)
	lcString = FGETS(lnFH, lnBlockSize)
	lnCurPos=1
	lnSize=1
	lnNoElements=11
	lnElement = 1
	DO WHILE lnElement <= lnNoElements
		lcChk=SUBSTR(lcString,lnCurPos,lnSize)
		IF lcChk=CHR(34) && Is alpha data so look for ending "
			lcString=SUBSTR(lcString,lnCurPos+1)
			lnFinPos=AT(CHR(34),lcString)
		ELSE
			lnFinPos=AT(CHR(44),lcString)
		ENDIF
		DO CASE
			CASE lnFinPos>lnCurPos+1 && Has data
				lcValue=SUBSTR(lcString,lnCurPos,lnFinPos-1)
			CASE lnFinPos=lnCurPos && No Data
				lcValue=""
			CASE lnFinPos = 0
				DO WHILE lnFinPos = 0 && Must be a CRLF pair within the String
					lcString = ALLTRIM(lcString) + FGETS(lnFH,lnBlockSize)
					IF lcChk=CHR(34)
					lnFinPos=AT(CHR(34),lcString)
					lcValue=SUBSTR(lcString,lnCurPos,lnFinPos-1)
					ELSE
					lnFinPos=AT(CHR(44),lcString)
					lcValue=SUBSTR(lcString,lnCurPos,lnFinPos-1)
					ENDIF
				ENDDO
		ENDCASE
			IF lcChk=CHR(34)
			lcString=SUBSTR(lcString,lnFinPos+2)
			ELSE
			lcString=SUBSTR(lcString,lnFinPos+1)
			ENDIF
		DO CASE
			CASE lnElement = 1
				lcf1 = lcValue
			CASE lnElement = 2
				lcf2 = lcValue
			CASE lnElement = 3
				lcf3 = lcValue
			CASE lnElement = 4
				lcf4 = lcValue
			CASE lnElement = 5
				lcf5 = lcValue
			CASE lnElement = 6
				lcf6 = lcValue
			CASE lnElement = 7
				lcf7 = lcValue
			CASE lnElement = 8
				lcf8 = lcValue
			CASE lnElement = 9
				lcf9 = lcValue
			CASE lnElement = 10
				lcf10 = lcValue
			CASE lnElement = 11
				lcf11 = lcValue
		ENDCASE
		lnElement=lnElement+1
	ENDDO
	INSERT INTO curTable (f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11) ;
		VALUES (lcf1,lcf2,lcf3,lcf4,lcf5,lcf6,lcf7,lcf8,lcf9,lcf10,lcf11)
ENDDO

BROWSE 

Fclose(lnFH)
Close Tables All
Release All
>Hi Neil,
>
>What about
>
>lcBuffer = FILETOSTR(lcFileName)
>lcBuffer = STRTRAN(lcBuffer, CHR(13) + CHR(10), "##CR##LF##")
>DIMENSION laLines[1]
>FOR i=1 TO ALINES(laLines, lcBuffer)
>  * Parse line here and convert "##CR##LF##" back to CHR(13) + CHR(10) for the memo
>  INSERT INTO ...
>ENDFOR
>
>
>>I am trying to code an import of a non standard CSV file.
>>The data starts at line 1 of the file.
>>The main problem is the second field contains commas & Carriage return line feed pairs within it and can be from 10 to 1200 characters in length.
>>
>>I have listed some sample data below.
>>
>>
>>"AA0001","AA0001 has the following classes, <CR><LF>1. Class1<CR><LF>2. Classes 2, 3 & 4<CR><LF>Blah Blah","600.00",60,,"AA001"<CR>
>>"AA0002","AA0002 has the following class, <CR><LF>1. Class1<CR><LF>Blah Blah","700.00",50,,"AA002"<CR>
>>
>>
>>Does anyone have any code on how to import the second field as FGETS truncations at the first and I need to import it into a Memo field
>>
>>Thanks in advance
Regards N Mc Donald
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform