ignore lines of csv that contain empty cells

ignore lines of csv that contain empty cells

mark_aldred
Advocate Advocate
1,311 Views
7 Replies
Message 1 of 8

ignore lines of csv that contain empty cells

mark_aldred
Advocate
Advocate

I'm reading from a csv file into a PFlow BirthScript operator. How can I ..

 

1. ignore lines that contain an empty cell or a specific character

2. alternatively just place a zero in the empty cell and read the line anyway

 

Thanks

 

 

filename = "C:\XXXX.csv"
FP = openfile filename
while eof(FP) == false do
(
numA = (readDelimitedString FP ",") as integer
numB = (readDelimitedString FP ",") as float
numC = (readLine FP) as float
)

 

 

0 Likes
1,312 Views
7 Replies
Replies (7)
Message 2 of 8

istan
Advisor
Advisor

I'd simply analyze the string which is returned from readDelimitedString.. e.g.

s = readDelimitedString fp ","

if s.count < 1 or s=="SPECIALCHAR" then s = "0"

 

Message 3 of 8

mark_aldred
Advocate
Advocate
Thanks!
How would I go about disregarding lines that are empty or contain a specific character?
0 Likes
Message 4 of 8

istan
Advisor
Advisor
Message 5 of 8

denisT.MaxDoctor
Advisor
Advisor

 


@mark_aldred wrote:

I'm reading from a csv file into a PFlow BirthScript operator. How can I ..

 

1. ignore lines that contain an empty cell or a specific character

2. alternatively just place a zero in the empty cell and read the line anyway

 

Thanks

 

 

 

filename = "C:\XXXX.csv"
FP = openfile filename
while eof(FP) == false do
(
numA = (readDelimitedString FP ",") as integer
numB = (readDelimitedString FP ",") as float
numC = (readLine FP) as float
)

 

 

 


MXS coerces null strings ("", " ", "  ", etc.) to Integer and Float as 0... Also, the coercion trims left and right null characters.

 

As I see from your original code you must read three numbers in the row (line) : integer, float, float.

 

So, the code might be as simple as:

 

 

fs = openfile "xxx.csv"
data = #()
while not eof fs do
(
	d = filterstring (readline fs) ","
	if d.count > 2 do
	(
		append data #(d[1] as integer, d[2] as float, d[3] as float)
	)
) 

 

 

  

Message 6 of 8

denisT.MaxDoctor
Advisor
Advisor

I see you are talking about the "PFlow BirthScript operator"... so reading and parsing speed may be critical for you. The code above is not fast enough for particularly large files. I recommend using .NET or Python to read all the lines from a file at once and parse the lines afterward.

Also, to speed up the process, you should keep condition checks to a minimum. For example, trust that there are at least three columns per row.

 

Message 7 of 8

istan
Advisor
Advisor

Another option would be to convert the .csv to a MXS file holding e.g.

data=#( #(1,2,3),  #(1,2,3) )

and to load it via filein()

Message 8 of 8

mark_aldred
Advocate
Advocate

Thanks for all the advice.

I'll go back and do some testing. I'd really appreciate your help if I come up against problems.

0 Likes