Hi all,
I'm using CsvFile to read some quite large CSV files and all is okay apart from CsvFile.Fields.
I notice in the documentation that field names are set to lower case, why is this?
In the files I am using all the field names are capitalised, some are all capitals (acronyms for example), so to have them convert to all lower case is causing a bit of a problem.
Is there a way of converting field names to their original format?
Thanks all
Pusherman
Using CsvFile.Fields
- BruceSteers
- Posts: 1608
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Using CsvFile.Fields
What about CsvFile.KeepNames
https://gambaswiki.org/wiki/comp/gb.uti ... /keepnames
https://gambaswiki.org/wiki/comp/gb.util/csvfile
https://gambaswiki.org/wiki/comp/gb.uti ... /keepnames
https://gambaswiki.org/wiki/comp/gb.util/csvfile
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: Using CsvFile.Fields
Thanks, but at the risk of sounding dumb I can't figure out what format KeepNames takes.
If I say myCSVfile.KeepNames=True then myCSVfile.Fields just returns #0
What am I missing, here?
If I say myCSVfile.KeepNames=True then myCSVfile.Fields just returns #0
What am I missing, here?
- BruceSteers
- Posts: 1608
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Using CsvFile.Fields
No idea, i've never used CsvFile
according to doc it's just a simple boolean switch.
I assume you have 3.17+wiki wrote: Property KeepNames As Boolean
Since 3.17
Return or set if field names are kept unchanged or not.
If this property is set, the NoDiacritics property is ignored.
It's possible there's a bug but nobody is really using the CsvFile.class so it's not been reported yet.
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: Using CsvFile.Fields
Thanks for looking, I'm on the latest version.
I have a work-around that's okay for me. I'm reading the first line of the file with a simple Line Input and substituting what I've read with what .Fields gives me.
Thanks, again.
I have a work-around that's okay for me. I'm reading the first line of the file with a simple Line Input and substituting what I've read with what .Fields gives me.
Thanks, again.
- cogier
- Site Admin
- Posts: 1129
- Joined: Wednesday 21st September 2016 2:22pm
- Location: Guernsey, Channel Islands
Re: Using CsvFile.Fields
I think there may be a bug here, as I get the same #0 that you did. But just for fun, here is my workaround.
- BruceSteers
- Posts: 1608
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Using CsvFile.Fields
It's definitely a bug.
I just submitted a fix.
https://gitlab.com/gambas/gambas/-/merge_requests/338
the code was this...
but should be this...
The sField was never set if using KeepNames so i moved it to before the If condition.
I've attached the fixed CsvFile.class if you fancy testing it works
I just submitted a fix.
https://gitlab.com/gambas/gambas/-/merge_requests/338
the code was this...
For iInd = 0 To $aField.Max
If Not $bKeepNames Then
sField = Trim($aField[iInd])
sField = Replace(sField, String.Chr(160), " ")
sField = Replace(sField, "\n", " ")
sField = Replace(sField, "\t", " ")
While InStr(sField, " ")
sField = Replace(sField, " ", " ")
Wend
sField = String.LCase(sField)
Endif
If $bNoDiacritics Then sField = String.RemoveDiacritics(sField)
If Not sField Then sField = "#" & CStr(iInd)
$aField[iInd] = sField
Next
but should be this...
For iInd = 0 To $aField.Max
sField = Trim($aField[iInd]) ' moved here
If Not $bKeepNames Then
sField = Replace(sField, String.Chr(160), " ")
sField = Replace(sField, "\n", " ")
sField = Replace(sField, "\t", " ")
While InStr(sField, " ")
sField = Replace(sField, " ", " ")
Wend
sField = String.LCase(sField)
Endif
If $bNoDiacritics Then sField = String.RemoveDiacritics(sField)
If Not sField Then sField = "#" & CStr(iInd)
$aField[iInd] = sField
Next
The sField was never set if using KeepNames so i moved it to before the If condition.
I've attached the fixed CsvFile.class if you fancy testing it works
- Attachments
-
- CsvFileFix.class.tar.gz
- (2.45 KiB) Downloaded 74 times
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- BruceSteers
- Posts: 1608
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Using CsvFile.Fields
Never mind testing the fix Ben has already accepted it so just download latest commit once the gitlab-ci has compiled it all.
If at first you don't succeed , try doing something differently.
BruceS
BruceS
- BruceSteers
- Posts: 1608
- Joined: Thursday 23rd July 2020 5:20pm
- Location: Isle of Wight
- Contact:
Re: Using CsvFile.Fields
That fix went straight into stable 3.19.1 so it's fixed now for latest master and stable
But...
If you may need your program to work on older gambas versions than 3.19.1 then do not use CsvFile.class from gb.util but place the CsvFileFix.class i uploaded into your programs source folder (rename it if you want) and use that class instead.
But...
If you may need your program to work on older gambas versions than 3.19.1 then do not use CsvFile.class from gb.util but place the CsvFileFix.class i uploaded into your programs source folder (rename it if you want) and use that class instead.
If at first you don't succeed , try doing something differently.
BruceS
BruceS
Re: Using CsvFile.Fields
Yes, the update works fine for me.
Thanks for your help, guys.
Thanks for your help, guys.