Subject Re: IMPORTING CSV file
From Lee Grant <camilee@nospam.comcast.net>
Date Thu, 11 Apr 2019 02:02:47 -0400
Newsgroups dbase.getting-started
Attachment(s) quoteclose.prg

Adding to that,

Since I needed to do what I said, I went ahead and created a small
program that just encloses the csv data in quotes so they can manually
append the file to a table they have to format the same way the file
presents the data, in character strings, so things like the periods,
percentage sign and other characters, other than the comma's and the
CR/LF can be brought in as is. They can do conversions once the raw data
has been imported. *WARNING* this is just a sample program and WILL
delete the converted .csv file if you run it again and create it
again...plus you need to rename or copy the file you want converted to
the "Test.csv" name...sloppy, but I was just making sure it did what I
wanted it to do. :)


Lee


On 4/5/2019 3:44 AM, Mervyn Bick wrote:
> On 2019-04-04 6:47 PM, Mustansir Ghor wrote:
>> Dear Mervyn
>>
>> Thank you.
>>
>> A year ago I was totally inclined to XDML but now I would wish OODML
>>
>> There is xyz.txt file generated by an equipment. This file store 15
>> parameters seperated by , . example
>>
>> 15.6,3.4,2.0,4.33,1,5,7.2,1.3,3.4,8.3,4.22,0.0%,3,6,1
>>
>>
>> I need to read this text file and store these data in a table using
>> rowset. So is the best option is to use file(0 function or there is
>> simpler way to do it.
>
> XDML will import your .csv file with three lines of code.
>
> use tablename
> append from whatever.csv delimited
> use
>
> OODML has no built in methods to do this so if you really want to use
> OODML you need to write code.
>
> One would use a file() object to read the .csv line by line.  One then
> needs to append a blank record and populate the fields by selecting the
> field value from the .csv record.  The value retrieved from the .csv
> file is a character string.  If the field is not a character field then
> the value needs to be converted before being placed in the table.
>
> In your case all the fields are numeric so there is not too much
> converting to do.
>
> A little example program is attached.
>
> Mervyn.
>
>
>
>
>




/*
   --------------------------------------------------------
   Filename.....: quoteclose.prg
   ClassName....:
   Purpose......: Create character fields of .csv files for
                  importing / appending to tables
   Programmer...: Lee Grant
   Date.........: 4/11/2019
   Note........: File in must be named "Test.csv", output file
                  will be named "Testconv.csv". Running the program again
                  will delete the previous "Testconv.csv" and create a new one
   Written for..: Practice and because of a question in the dBASE Newsgroups
   Rev. History.: 1.0 finished working version after trial and errors
   Dependencies.:
   HelpFile.....:
   Example:.....:
   --------------------------------------------------------
   Notes: this isn't fancy, it takes a file in the current directory and will
            process it creating another file with the converted .csv file.
            dBASE won't append mixed data, so this converts the fields in the
            file to charcter fields by enclosing them in quotes. To append them
            into a new database, you need to create the fields in the format
            in a new database that you want them before appending with this file
            ..its really basic and handles only the CR/LF combo of CHR(13)+CHR(10)
            to read in characters and process it.
      --------------------------------------------------------
   */
// Create the constants so we know what filenames we are going to work on. The
// incoming file will be named cTest, and the output file, cConv.
cTest = "test.csv"
cConv = "testconv.csv"
// We need two file objects. One to read in the importing file and one to
// write out the result after transformation.
fIn = new file()
fOut = new file()
// open the incoming file
fIn.open(cTest , "R")
// for this time, we're just going to create an arbitrary file to copy into
// so if it exists, delete it and create a new one.
if fOut.exists(cConv)
   fOut.delete(cConv)
endif
   fOut.create(cConv, "W")


// Need a variable to check for a "," and if so, file.write() a open/close quote
// and then the variable holding the comma delimiter.
local cIn, cOut
cOut = '"'
// Now the file needs to be parsed per character and processed per comma entry
// so start at begining of input file, start the clean file with an open quote
// to start us off
fIn.seek(0)
fOut.write(cOut)

do while not fIn.eof()
   cIn = fIn.read(1)
   if cIn == ","  // Comma-seperated value,  
      fOut.write(cOut) // close the quote,
      fOut.write(cIn) // write comma,
      fOut.write(cOut) // open quote.
   elseif cIn == CHR(13) // CR/LF pair, put in close quote here before feed
      fOut.write(cOut)
      fOut.write(cIn)
   elseif cIn == CHR(10) // CR/LF pair, put in open quote here after feed.
      fOut.write(cIn)
      fOut.write(cOut)
   else
      fOut.write(cIn) // Write the current character, it's part of the data.
   endif
enddo

fOut.write(cOut) // We're at the end of the incoming file, so..let's close the
                 // the last field up with a closing quote.

// We're done copying the file over position by position, so now close the files
// release the variables and exit.
fOut.close()
fIn.close()
release fIn, fOut, cIn, cOut

// done
return