Results 1 to 2 of 2

Thread: Parsing CSV data file

  1. #1
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,817
    Rep Power
    10

    Parsing CSV data file

    How many times did you try to load a csv data file? Those files with a comma (or other char) delimited fields? How many For/Next loops did you use?
    What about just few lines of code to do the job? Here an example and the CSV sample data is attached.
    This script has more than few lines just to show you in details the process but all the job is done by the following single line:

    [code=thinbasic]

    PARSE(FILE_Load(FileToLoad), MyMatrix(), $crlf , ",")

    [/code]

    _________________________________________________

    [code=thinbasic]
    uses "file"
    uses "console"

    dim FileToLoad as string value app_sourcepath & "CSV_SampleData.csv"
    dim MyMatrix() as string
    dim nLines as long
    dim nCols as long
    dim T0, T1 as double

    console_writeline("This script demonstrate the use of PARSE function for loading, parsing and filling a string array with the content of a CSV file. All with only one line of code.")
    console_writeline("Input file: " & FileToLoad)
    console_writeline("File size : " & file_size(FileToLoad) & " bytes")
    console_writeline("Press any key to start.")
    console_writeline(repeat$(79, "-"))
    console_waitkey

    '---
    '---Starting to load and parse input file
    '------
    T0 = timer
    '---
    '---Just one line do the job of loading file data, parsing text lines, dimensioning and filling the matrix.
    '------
    PARSE(FILE_Load(FileToLoad), MyMatrix(), $crlf , ",")

    '--Now get the number of lines and max number of columns parsed
    nLines = ubound(MyMatrix(1))
    nCols = ubound(MyMatrix(2))

    T1 = timer

    '---Write some info
    console_writeline("Total loading and parsing time: " & format$(T1 - T0, "#0.000000") & " secs")
    console_writeline("Total number of lines : " & nLines)
    console_writeline("Total number of colums: " & nCols)
    console_writeline("Press any key to show result on screen")
    console_writeline(repeat$(79, "-"))
    console_waitkey

    dim CountLine as long
    dim CountCol as long

    for CountLine = 1 to nLines
    for CountCol = 1 to nCols
    console_write(MyMatrix(CountLine, CountCol) & " ")
    next
    console_writeline("")
    next

    console_writeline(repeat$(79, "-"))
    console_writeline("Program terminated. Press any key to close.")
    console_waitkey
    [/code]
    Attached Files Attached Files
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  2. #2
    thinBasic MVPs kryton9's Avatar
    Join Date
    Nov 2006
    Location
    Naples, Florida & Duluth, Georgia
    Age
    68
    Posts
    3,865
    Rep Power
    405

    Re: Parsing CSV data file

    This is very very useful Eros, thanks!!
    Acer Notebook: Win 10 Home 64 Bit, Core i7-4702MQ @ 2.2Ghz, 12 GB RAM, nVidia GTX 760M and Intel HD 4600
    Raspberry Pi 3: Raspbian OS use for Home Samba Server and Test HTTP Server

Similar Threads

  1. data file produces runtime error
    By efly2020 in forum thinBasic General
    Replies: 2
    Last Post: 26-10-2008, 20:19

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •