CSV File help
Posted
#1
(In Topic #1422)
Regular

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.
Posted
Regular

https://gambas-buch.de…d=k17:k17.7:k17.7.6:start
b
p.s. to delete a file, just use Kill(path).
Posted
Guru

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.
Posted
Regular

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
Posted
Guru

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
Posted
Regular

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
Posted
Regular

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.
Posted
Guru

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…
Posted
Regular

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
Posted
Guru

Somehow i missed the square brackets :roll:
Posted
Regular

I am back to field property must be set first.
Lucille
Posted
Guru

Posted
Regular

Lucille
Posted
Regular

3 more files coming.
Lucille
Posted
Regular

Posted
Guru

Code (gambas)
- writer = New CsvFile(Application.Path &/ "recbred.csv") ' create new csv file using "writer" as the reference
- writer.Fields = aFields ' set the fields to be what aFields is.
- writer.Close ' close the file
Posted
Regular

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
Posted
Guru

Code (gambas)
- writer = CsvFile.Create(Application.Path &/ "recbred.csv") ' create new csv file using "writer" as the reference
- writer.Fields = aFields ' set the fields to be what aFields is.
- writer.Close ' close the file
Posted
Regular

So in short, use
Code (gambas)
Code (gambas)
Here is a complete program to do what you want i.e. BruceS' code that works:
Code (gambas)
- ' Gambas module file
- writer.Fields = aFields ' set the fields to be what aFields is.
- writer.Close ' close the file
<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>
- <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>
- <LI>The CSVFile.Create will SILENTLY KILL any existing file of the same. Beware!</LI>
b
Argh Ya beat me to it again!
Posted
Regular

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
Posted
Regular

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
Posted
Regular

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
Posted
Guru

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)
- '' Open an existing csv file for further writing.
- ''
- '' This renames a file and and re-creates it opened for writing like CsvFile.Create with the existing data already written.
- hNewCsv.Fields = hOldCsv.Fields ' duplicate fields
- Do ' copy existing data...
- hOldCsv.Close ' close Path.old and delete it
- Return hNewCsv
Have fun
Posted
Regular

Thx for pointing this out.
Lucille
1 guest and 0 members have just viewed this.










