Using CsvFile.Fields

Post

Posted
Rating:
#1 (In Topic #1206)
Trainee
 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
Online now: No Back to the top

Post

Posted
Rating:
#2
Online now: No Back to the top

Post

Posted
Rating:
#3
Trainee
 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?
Online now: No Back to the top

Post

Posted
Rating:
#4
Guru
BruceSteers is in the usergroup ‘Guru’

pusherman said

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 said

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.
Online now: No Back to the top

Post

Posted
Rating:
#5
Trainee
 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.
Online now: No Back to the top

Post

Posted
Rating:
#6
Avatar
Guru
cogier is in the usergroup ‘Guru’
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.

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#7
Guru
BruceSteers is in the usergroup ‘Guru’
It's definitely a bug.

I just submitted a fix.
Update CsvFile.class, fix error when using KeepNames (!338) · Merge requests · Gambas / gambas · GitLab

the code was this…

Code (gambas)

  1.   For iInd = 0 To $aField.Max
  2.    
  3.     If Not $bKeepNames Then
  4.    
  5.       sField = Trim($aField[iInd])
  6.      
  7.       sField = Replace(sField, String.Chr(160), " ")
  8.       sField = Replace(sField, "\n", " ")
  9.       sField = Replace(sField, "\t", " ")
  10.      
  11.       While InStr(sField, "  ")
  12.         sField = Replace(sField, "  ", " ")
  13.       Wend
  14.      
  15.       sField = String.LCase(sField)
  16.      
  17.     Endif
  18.    
  19.     If $bNoDiacritics Then sField = String.RemoveDiacritics(sField)
  20.    
  21.     If Not sField Then sField = "#" & CStr(iInd)
  22.     $aField[iInd] = sField
  23.   Next
  24.  

but should be this…

Code (gambas)

  1.   For iInd = 0 To $aField.Max
  2.    
  3.     sField = Trim($aField[iInd])  ' moved here
  4.      
  5.     If Not $bKeepNames Then
  6.    
  7.       sField = Replace(sField, String.Chr(160), " ")
  8.       sField = Replace(sField, "\n", " ")
  9.       sField = Replace(sField, "\t", " ")
  10.      
  11.       While InStr(sField, "  ")
  12.         sField = Replace(sField, "  ", " ")
  13.       Wend
  14.      
  15.       sField = String.LCase(sField)
  16.      
  17.     Endif
  18.    
  19.     If $bNoDiacritics Then sField = String.RemoveDiacritics(sField)
  20.    
  21.     If Not sField Then sField = "#" & CStr(iInd)
  22.     $aField[iInd] = sField
  23.   Next
  24.  

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

Attachment
Online now: No Back to the top

Post

Posted
Rating:
#8
Guru
BruceSteers is in the usergroup ‘Guru’
Never mind testing the fix Ben has already accepted it so just download latest commit once the gitlab-ci has compiled it all.
Online now: No Back to the top

Post

Posted
Rating:
#9
Guru
BruceSteers is in the usergroup ‘Guru’
 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.
Online now: No Back to the top

Post

Posted
Rating:
#10
Trainee
:) Yes, the update works fine for me.

Thanks for your help, guys.
Online now: No Back to the top

Post

Posted
Rating:
#11
Guru
BruceSteers is in the usergroup ‘Guru’
 Your welcome.

Note:  there was a bug in the fixed file i uploaded because i gave it a different name.

Commands like CsvFileFix.Open() returned a CsvFile object not a CsvFileFix object because i did not replace all the "CsvFile" text in the class to be "CsvFileFix"

So i re-uploaded the fixed version , but if you rename the class file you must change all the words in the class from "CsvFileFix" to whatever you name it.
Online now: No Back to the top
1 guest and 0 members have just viewed this.