Using CsvFile.Fields

New to Gambas? Post your questions here. No question is too silly or too simple.
pusherman
Posts: 16
Joined: Sunday 5th November 2023 11:45am

Using CsvFile.Fields

Post by pusherman »

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
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using CsvFile.Fields

Post by BruceSteers »

If at first you don't succeed , try doing something differently.
BruceS
pusherman
Posts: 16
Joined: Sunday 5th November 2023 11:45am

Re: Using CsvFile.Fields

Post by pusherman »

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?
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using CsvFile.Fields

Post by BruceSteers »

pusherman wrote: Friday 15th March 2024 9:18pm 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?

No idea, i've never used CsvFile

according to doc it's just a simple boolean switch.
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.
I assume you have 3.17+

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
pusherman
Posts: 16
Joined: Sunday 5th November 2023 11:45am

Re: Using CsvFile.Fields

Post by pusherman »

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.
User avatar
cogier
Site Admin
Posts: 1127
Joined: Wednesday 21st September 2016 2:22pm
Location: Guernsey, Channel Islands

Re: Using CsvFile.Fields

Post by cogier »

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.
CSVTest-0.0.1.tar.gz
(8.54 KiB) Downloaded 366 times
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using CsvFile.Fields

Post by BruceSteers »

It's definitely a bug.

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 55 times
If at first you don't succeed , try doing something differently.
BruceS
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using CsvFile.Fields

Post by BruceSteers »

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
User avatar
BruceSteers
Posts: 1578
Joined: Thursday 23rd July 2020 5:20pm
Location: Isle of Wight
Contact:

Re: Using CsvFile.Fields

Post by BruceSteers »

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.
If at first you don't succeed , try doing something differently.
BruceS
pusherman
Posts: 16
Joined: Sunday 5th November 2023 11:45am

Re: Using CsvFile.Fields

Post by pusherman »

:) Yes, the update works fine for me.

Thanks for your help, guys.
Post Reply