Alguna idea de como leer desde FW un archivo de texto CSV para convertirlo a DBF?
Si uso FOpen() y leo el archivo (fichero) de miles de records, linea a linea, se hace muy lento el proceso.
Saludos,
George
include 'fivewin.ch'
#include 'xbrowse.ch'
function Main()
local cBuf
local aData, aHeaders
cBuf := MemoRead( 'input.txt' )
// parse and make a multi dimentional array
if cBuf[ -1 ] == 26 // check and remove Ctrl-Z
cBuf := Left( cBuf, Len( cBuf ) - 1 )
endif
if Right( cBuf, 2 ) != CRLF
// pad with CRLF if needed, not to miss the last line
cBuf += CRLF
endif
cBuf := StrTran( cBuf, CRLF, Chr( 10 ) )
aData := hb_aTokens( cBuf, Chr(10) )
AEval( aData, { |c,i| aData[ i ] := hb_aTokens( c, ';' ) } )
aHeaders := aData[ 1 ]
aData := ADel( aData, 1 )
aSize( aData, Len( aData ) - 1 )
// Parsing is done
// now view the data quickly in xbrowse before writing in DBF
xBrowse( aData, ; // data to browse
"Parsed Data as Array", ; // title
.f., ; // no autosort
{ |oBrw| AEval( oBrw:aCols, { |o,i| o:cHeader := aHeaders[ i ] } ) } ;
) // codeblock above assigns header names
// Extending the program to Write DBF
WriteToDBF( 'INPUT.DBF', aData, aHeaders )
// Check the DBF
USE INPUT
XBrowse()
return nil
static function WriteToDBF( cDbf, aData, aHeaders )
local aStruct := {}
local n, nLen := Len( aData )
AEval( aHeaders, ;
{ |c| AAdd( aStruct, ;
{ Upper( Left( Trim( c ), 10 ) ), 'C', 2, 0 } );
} )
// desirable to remove embedded chars not acceptable in fieldnames
// and to check for duplicates
// that logic is not included here
for n := 1 to nLen
AEval( aData[ n ], ;
{ |c,i| c := Trim( c ), ;
aStruct[ i ][ 3 ] := Max( Len( c ), aStruct[ i ][ 3 ] ), ;
aData[ n ][ i ] := c ;
} )
next n
dbCreate( cDbf, aStruct )
USE ( cDbf ) NEW ALIAS OUT EXCLUSIVE
for n := 1 to nLen
OUT->( dbAppend() )
AEval( aData[ n ], { |c,i| OUT->( FieldPut( i, c ) ) } )
next
OUT->( dbCloseArea() )
return nil
Return to FiveWin para Harbour/xHarbour
Users browsing this forum: Google [Bot] and 64 guests