an easy way to load a CSV File

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by scurrier.

an easy way to load a CSV file example:
file name sean.csv
sean,currier,33,7-23-69
is in the file

commands
opencsv(1,"sean.csv")
readcsvs (1,first$,last$,age,date$)
and maybe a writecsv would be great too

thanks
Sean
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by Andre.

An easy way to do this (not tested):

Code: Select all

Structure person
  FirstName$
  Name$
  Age.b
  Date$
EndStructure
NewList Persons.person()

If ReadFile(0,"sean.csv")
  Repeat
    a$=ReadString()
    AddElement(Persons())
    Persons()\FirstName$=StringField(a$,1,",")
    Persons()\Name$=StringField(a$,2,",")
    Persons()\Age=Val(StringField(a$,3,","))
    Persons()\Date$=StringField(a$,4,",")
  Until Eof(0)  
  CloseFile(0)
EndIf

; "sean.csv" file must look like:
; sean,currier,33,7-23-69
; ....
The Write function should be no problem too :)
This way I always use lots of my csv files in an absolute flexible way...

Regards
André

*** German PureBasic Support ***
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by fred.

Yes, the StringField() function is the way to go, and with a for/next it's nearly immediate.

Fred - AlphaSND
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by scurrier.

Thanks
This helps a lot.
Hay Fred how about the remstart and remend in the next version?
you could do it like in C
\\ starts the rem and // ends the rem
Post Reply