CSV File help

Post

Posted
Rating:
#1 (In Topic #1422)
Regular
tailkinker is in the usergroup ‘Regular’
 Greetings!

After figuring out how to create a .csv file, I am stumped on the syntax for creating fields and then writing data to the .CSV file.  Also, how to delete CSV files.

Thx.
Online now: No Back to the top

Post

Posted
Rating:
#2
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
This may help. I am currently (at a low priority) looking at it myself.
https://gambas-buch.de…d=k17:k17.7:k17.7.6:start
b

p.s. to delete a file, just use Kill(path).

Online now: No Back to the top

Post

Posted
Rating:
#3
Avatar
Guru
cogier is in the usergroup ‘Guru’
 I use CSV files all the time. They are easy to understand and Gambas opens CSV file with 6500 lines of data effortlessly.

I am happy to help you, but can you expand on what you are trying to accomplish so I can provide a more accurate response.
Online now: No Back to the top

Post

Posted
Rating:
#4
Regular
tailkinker is in the usergroup ‘Regular’
 Cogier,

I am writing a program that pulls highly detailed nutrient information from a csv file (I already have the csv files for that - the copyright permission is public domain).  

 I want to sort this all out by food groups and then also different .csv files for recipes with common ingredients etc.  I am using gb.util and so far I can open the existing file, get the info I need, and display it in a text area.  Now I want to create a new .csv file (done) and add the pulled data to that CSV (this is where I am stuck.)

I am still deciding between writing into a collection or an array, but for this test piece, wrote into an array.

It's burping at an error related to no fields defined in the .csv file.

Thx
Online now: No Back to the top

Post

Posted
Rating:
#5
Guru
BruceSteers is in the usergroup ‘Guru’
So did you set any fields?
The wiki says you need to set fields before writing data.
/comp/gb.util/csvfile/fields - Gambas Documentation

have you had a good read of all the CsvFile.class wiki ?  /comp/gb.util/csvfile - Gambas Documentation
Online now: No Back to the top

Post

Posted
Rating:
#6
Regular
tailkinker is in the usergroup ‘Regular’
 Bruce,

No. I haven't set the fields.  I did look at the wiki, but didn't realize that is how to set the fields in the file.  I will give that a try.

Thx

Lucille
Online now: No Back to the top

Post

Posted
Rating:
#7
Regular
tailkinker is in the usergroup ‘Regular’
 I am still missing something.

This is the code I have.

Dim recbreads as csvfile

Property fields as string[] = [ "ingredients", "carbs", "unit1"]   #### This line is giving me an unexpected property (Fmain.class:177) error.

Dim sendout as variant[ ] = ["flour", "45", "grams"]


recbreads = new csvfile(Application.path & "/" & "breads.csv")

recbreads.write(sendout) #### without the property line, I get the error "the field property must be set first (Fmain: 193). It does create the csv file, just can't seem to get anything into it.
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
You are using Property wrong.

For a simple variable you probably just want to use a Public or Private definition..

eg
Private Fields as string[] = [ "ingredients", "carbs", "unit1"]

or…
Public Fields as string[] = [ "ingredients", "carbs", "unit1"]

Or for an actual "Property" use the "Use" keyword to do it simply like this..

Property Fields as string[] Use $aFields = [ "ingredients", "carbs", "unit1"]

or the old (complete) way like this…

Code (gambas)

  1. Property Fields as string[]
  2. Private $aFields As String[] = [ "ingredients", "carbs", "unit1"]
  3.  
  4. Private Function Fields_Read() As String[]
  5.  
  6.   Return $aFields
  7.  
  8.  
  9. Private Sub Fields_Write(Value As String[])
  10.  
  11.   $aFields = Value
  12.  
  13.  
Online now: No Back to the top

Post

Posted
Rating:
#9
Regular
tailkinker is in the usergroup ‘Regular’
 Still no joy.

I am getting the error "type mismath wanted string and got string[] instead.  

I am getting that when I add the private $afields line to the class section.

Lucille
Online now: No Back to the top

Post

Posted
Rating:
#10
Guru
BruceSteers is in the usergroup ‘Guru’
Spot the obvious correction  :D

Somehow i missed the square brackets :roll:
Online now: No Back to the top

Post

Posted
Rating:
#11
Regular
tailkinker is in the usergroup ‘Regular’
 Some joy but no seasons in the sun.

I am back to field property must be set first.

Lucille
Online now: No Back to the top

Post

Posted
Rating:
#12
Guru
BruceSteers is in the usergroup ‘Guru’
Post your code and we can shine some light on it for you <EMOJI seq="1f60e" tseq="1f60e">😎</EMOJI>
Online now: No Back to the top

Post

Posted
Rating:
#13
Regular
tailkinker is in the usergroup ‘Regular’
 Ok, will tend this in the morning.  Hoping this isn't an issue with my install.

Lucille
Online now: No Back to the top

Post

Posted
Rating:
#14
Regular
tailkinker is in the usergroup ‘Regular’
Had a few minutes, so here are the screen captures of my code, and also Gambas version that I am running.


Image

(Click to enlarge)


Image

(Click to enlarge)


Image

(Click to enlarge)


3 more files coming.



Lucille
Online now: No Back to the top

Post

Posted
Rating:
#15
Regular
tailkinker is in the usergroup ‘Regular’
Last 3

Image

(Click to enlarge)


Image

(Click to enlarge)


Image

(Click to enlarge)

Online now: No Back to the top

Post

Posted
Rating:
#16
Guru
BruceSteers is in the usergroup ‘Guru’
Maybe this makes sense..

Code (gambas)

  1.  
  2. Private aFields As String[] = ["ingredients", "carbs", "units"]  ' this is just a string array
  3.  
  4. Public Sub writecsv_Click()
  5.  
  6.   Dim writer As CsvFile
  7.  
  8.   Dim sendout As String[] = ["flour", "45 grams", "16 grams"] ' the array to send at write time.
  9.  
  10.   writer = New CsvFile(Application.Path &/ "recbred.csv") ' create new csv file using "writer" as the reference
  11.  
  12.   writer.Fields = aFields  ' set the fields to be what aFields is.
  13.  
  14.   writer.Write(sendout)  ' write the string array.
  15.  
  16.   writer.Close  '  close the file
  17.  
  18.  
  19.  
Online now: No Back to the top

Post

Posted
Rating:
#17
Regular
tailkinker is in the usergroup ‘Regular’
 I had to add back in the create statement.  

Now I get on the writer.fields = a fields    #### read only property Fmain 221.

It's a different error, so that's a good thing, if not quite joy.

Lucille
Online now: No Back to the top

Post

Posted
Rating:
#18
Guru
BruceSteers is in the usergroup ‘Guru’
aah yes sorry, i should not have used "New CsvFile()" to create the reference.

Code (gambas)

  1.  
  2. Private aFields As String[] = ["ingredients", "carbs", "units"]  ' this is just a string array
  3.  
  4. Public Sub writecsv_Click()
  5.  
  6.   Dim writer As CsvFile
  7.  
  8.   Dim sendout As String[] = ["flour", "45 grams", "16 grams"] ' the array to send at write time.
  9.  
  10.   writer = CsvFile.Create(Application.Path &/ "recbred.csv") ' create new csv file using "writer" as the reference
  11.  
  12.   writer.Fields = aFields  ' set the fields to be what aFields is.
  13.  
  14.   writer.Write(sendout)  ' write the string array.
  15.  
  16.   writer.Close  '  close the file
  17.  
  18.  
  19.  
Online now: No Back to the top

Post

Posted
Rating:
#19
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
Ah the wonderful world of Gambas help. I say it and say it over and over, but no-one listens: "Read EVERY word of the help".

Image

(Click to enlarge)

versus
Image

(Click to enlarge)


So in short, use

Code (gambas)

  1. writer = CSVFile.Create(...
because you want to write to it. Do not use

Code (gambas)

  1. writer = New CsvFile(Application.Path &/ "recbred.csv")
as well! That is why the headers are read-only.

Here is a complete program to do what you want i.e. BruceS' code that works:

Code (gambas)

  1. ' Gambas module file
  2.  
  3. Private aFields As Variant[] = ["ingredients", "carbs", "units"]  ' this is just a string array
  4.  
  5. Public Sub Main()
  6.  
  7.   Dim writer As CsvFile
  8.  
  9.   Dim sendout As String[] = ["flour", "45 grams", "16 grams"] ' the array to send at write time.
  10.  
  11.   writer = CsvFile.Create(Application.Path &/ "recbred.csv") ' create the new file OBJECT
  12.  
  13.   writer.Fields = aFields  ' set the fields to be what aFields is.
  14.  
  15.   writer.Write(sendout)  ' write the string array.
  16.  
  17.   writer.Close  '  close the file
  18.  
and a couple of notes:
<LIST>
  • <LI>The gb.util CSVFile class does not appear to allow you to open a CSV file object for, say, adding new data at the end of the file. It's a single shot concept.</LI>
</LIST>
<LIST>
  • <LI>It is also necessary to grasp the concept that the CSVFile class methods are NOT the same as their normal File counterparts.</LI>
</LIST>
<LIST>
  • <LI>The CSVFile.Create will SILENTLY KILL any existing file of the same. Beware!</LI>
</LIST>

b

Argh Ya beat me to it again!

Online now: No Back to the top

Post

Posted
Rating:
#20
Regular
tailkinker is in the usergroup ‘Regular’
 Bruce,

Good morning!

We have joy, we have fun, we have seasons in the sun!

It works!!

Thankyou again for your help.

Next up, among other things is use a database to populate line 1, as there are different nutrient combinations I plan to use.  The complete list, including amino acids, vitamins, and minerals is somewhere around 150 from the USDA.  Some places I only need around 50, but for full recipe testing sometimes I need to go much more to get the flavors and mouth feel.

Lucille
Online now: No Back to the top

Post

Posted
Rating:
#21
Avatar
Regular
thatbruce is in the usergroup ‘Regular’
 Lucille,
An aside.
I hope you are aware that the USDA food data is mainly based on the manufacturers declared data and not on independent testing. A few years ago I was looking at a similar thing and was quite disappointed at their data. I experienced some fairly obvious discrepancies with manufactured food products where even the declared ingredients were at odds with the final product composition. I don't think that is a severe fault with the Administration, apart from that they do not make that clear and obvious. Then, if you are only looking at raw ingredient data it may be more accurate.
I'll see if I can dig up a few of those old projects that looked at the US figures verses the European (and other) databases over the next week or so.
regards
thatbruce

Online now: No Back to the top

Post

Posted
Rating:
#22
Regular
tailkinker is in the usergroup ‘Regular’
 Bruce,

I didn't know that about the prepared foods.  The data I'm most using is non-processed foods like fruits and vegetables, then with some processing like flour.  I would love to see the European comparisons.  

Thx,

Lucille
Online now: No Back to the top

Post

Posted
Rating:
#23
Guru
BruceSteers is in the usergroup ‘Guru’
What thatbruce says about the CsvFile class not having a way to add/append to existing files is a good point.
bit of a downside really :(

So here's a simple function i quickly made to workaround that problem.  :)

it works the same as CsvFile.Open() / CsvFile.Create() and takes the same arguments and returns a pointer to the CsvFile file stream.,

the difference is it first renames the existing file, then uses the filename to create a new file.
then it writes the Fields/contents to the new file and closes/deletes the old one.

Code (gambas)

  1.  
  2. '' Open an existing csv file for further writing.
  3. ''
  4. '' This renames a file and and re-creates it opened for writing like CsvFile.Create with the existing data already written.
  5. Static Public Sub AppendCsv(Path As String, Optional (Separator) As String, Escape As String, CharSet As String) As CsvFile
  6.  
  7.   Dim hOldCsv As CsvFile
  8.   Dim hNewCsv As CsvFile
  9.  
  10.   Move Path To Path & ".old" ' rename Path to Path.old and open Path.old for read
  11.   hOldCsv = CsvFile.Open(Path & ".old", Separator, Escape, CharSet)
  12.  
  13.   hNewCsv = CsvFile.Create(Path, Separator, Escape, CharSet)' open Path fresh for write
  14.  
  15.   hNewCsv.Fields = hOldCsv.Fields ' duplicate fields
  16.  
  17.   Do  ' copy existing data...
  18.     Try hNewCsv.Write(hOldCsv.Read())
  19.     If Error Or If hOldCsv.Eof Then Break
  20.   Loop
  21.  
  22.   hOldCsv.Close ' close Path.old and delete it
  23.   Kill Path & ".old"
  24.  
  25.   Return hNewCsv
  26.  
  27.  
  28.  


Have fun :)
Online now: No Back to the top

Post

Posted
Rating:
#24
Regular
tailkinker is in the usergroup ‘Regular’
 I didn't try editing an existing .csv, so didn't realize I would need a workaround.  Will give this a spin tomorrow.

Thx for pointing this out.

Lucille
Online now: No Back to the top
1 guest and 0 members have just viewed this.