Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
CSV example: load, get cell, set cell, save
#1
Quote:Let's say I have a CSV file as such:



Filename: file.cvs



Directory = C:\CVSdb



CSV divider is "|" (and not a comma)



The file has 4 categories/columns as listed below & 2 rows:



Col-1 Col-2 Col-3 Col-4

LastName FirstName Age Sex



Ex:

Doe | John | 39 | M

Doe | Jane | 38 | F



Gintarus, could you provide me with a short example of how to:

-create this

-set the data (for example, change the name 'Jane' to 'Susan')

-get data = turn 'Susan' into a variable and then show it via 'out'
#2
Macro Macro2448
Code:
Copy      Help
;a CSV string for testing
str csv=
;Doe | John | 39 | M
;Doe | Jane | 38 | F

;create ICsv variable and load data
ICsv x._create
x.Separator="|" ;;change separator (default is ",")
x.FromString(csv) ;;or x.FromFile("C:\CVSdb\file.cvs")

;get a cell
str cell
cell=x.Cell(1 1) ;;0-based row and column (Jane)
out F"cell 1 1 was: {cell}"

;change cell
cell="Susan"
x.Cell(1 1)=cell

;get CSV data to string
x.ToString(csv) ;;or x.ToFile("C:\CVSdb\file.cvs")

;results
out "changed CSV:"
out csv
#3
Ahh!!! -perfect! Now I'm on my way with CSV elements. Thank You
#4
Excellent mini tutorial, it would be great if you could post further similar snippets now and again.


Forum Jump:


Users browsing this thread: 1 Guest(s)