Question Ephraim Malane · Oct 19, 2022

Remove text within quotes in a string

I am reading file values by position with comma-separated string and it gives me incorrect values on the below line because there is a comma within double quotes within a string.

I would like to remove any text that has quotes with object script or alternatively separate each value with pipe-delimited so that I can return position 8 as code and position 9 as a description

line = file.ReadLine()

description = $P(line,",","9")
code = $P(line,",","8")

12162,CHAPTER I,Certain infectious and parasitic diseases (A00-B99),003 (A20-A28),Certain zoonotic bacterial diseases,A28,"Other zoonotic bacterial diseases, not elsewhere classified",A28,"Other zoonotic bacterial diseases, not elsewhere classified",N,N,N,N,N,,,,,,,,,,

Product version: IRIS 2020.2

Comments

Julius Kavay · Oct 19, 2022

Assuming, fields which contains commas are quoted ("aaa,bbb,ccc") and (for the simplicity) fields does not contains quotes, then something like this should do the job


ClassMethod CSV(filename)
{
	s old=$system.Process.SetZEOF(1)	// use $zeof instead of error trap
	s result=[]
	o filename:"r":0
	i $t {
		u filename
		while '$zeof {
			read line
			i line]"" do result.%Push(..fields(line)) // ignore empty lines
		}
	}
	c filename
	d $system.Process.SetZEOF(old)
	q result
}

ClassMethod fields(line)
{
	s a="", f=0, row=[]
	f i=1:1:$l(line) {
		s c=$a(line,i)
		i c=44,'f d row.%Push(a) s a="" continue
		i c=34 s f='f continue
		s a=a_$c(c)
	}
	q row
}

A test output:


USER>s res=##class(DC.Help).CSV(fn)

USER>zso res
(0).(0).............: 12162
(0).(1).............: CHAPTER I
(0).(2).............: Certain infectious and parasitic diseases (A00-B99)
(0).(3).............: 003 (A20-A28)
(0).(4).............: Certain zoonotic bacterial diseases
(0).(5).............: A28
(0).(6).............: Other zoonotic bacterial diseases, not elsewhere classified
(0).(7).............: A28
(0).(8).............: Other zoonotic bacterial diseases, not elsewhere classified
(0).(9).............: N
(0).(10)............: N
(0).(11)............: N
(0).(12)............: N
(0).(13)............: N
(0).(14)............:
(0).(15)............:
(0).(16)............:
(0).(17)............:
(0).(18)............:
(0).(19)............:
(0).(20)............:
(0).(21)............:
(0).(22)............:

0
Vitaliy Serdtsev · Oct 20, 2022

Here are two ways:

s="12162,CHAPTER I,Certain infectious and parasitic diseases (A00-B99),003 (A20-A28),Certain zoonotic bacterial diseases,A28,""Other zoonotic bacterial diseases, not elsewhere classified"",A28,""Other zoonotic bacterial diseases, not elsewhere classified"",N,N,N,N,N,,,,,,,,,,G"
##class(%DeepSee.TermList).%ParseCSVRecord(s,.arr1)
zw arr1

!

list=$$CSVtoList^%occLibrary(s)

##class(%ListOfDataTypes).BuildValueArray(list,.arr2)
zw arr2


Take a look at the class methods %SQL.Util.Procedures

0