Published on InterSystems Developer Community (https://community.intersystems.com)

Home > How to read CSV file contents into Objects in Cache

Question
Thembelani Mlalazi · Oct 9, 2017

How to read CSV file contents into Objects in Cache

I have this code here;I am trying to read a csv file but when I run this code I get  <LIST>%open+3^%stream.Object.1 would anyone have a look and tell me where I am going wrong .Record mapper is fine but I need a custom csv Importer

ClassMethod ImportCSV()
{
   set adapter =##class(%File).%New()
  
  set status=adapter.%Open("C:\In\in.csv")
  
  if $$$ISERR(status)
  {
   do $System.Status.DisplayError(status)
  }
  
  set counter =1
  
  while 'adapter.AtEnd
  {
   set line=adapter.ReadLine()
   
   set rec= ##class(CSVRecord).%New()
   
   set rec.ID=$piece(line,",",1)
   set rec.FName=$piece(line,",",2)
   set rec.SecName=$piece(line,",",3)
   set rec.Gender=$piece(line,",",4)
   set rec.Age=$piece(line,",",5)
   
     write rec.ID,
 
             rec.FName,

             rec.Secname ,

             rec.Gender,

             rec.Age,!
        
         Set savestatus=rec.%Save()
              
      if $$$ISERR(savestatus)
    {
     do $System.Status.DisplayError(status)
    }
   
    }
   
    //if error found during processing,show it
    if $$$ISERR(status)
    {
     do $System.Status.DisplayError(status)
    }
}

#CSV #Object Data Model #Caché

Source URL:https://community.intersystems.com/post/how-read-csv-file-contents-objects-cache