I thought I might have to do strange things with CSV files one day, and today was the day. I was importing some transactions from Fidelity into composites for charting. Here's a sample of the CSV file:
Date,Investment,Transaction Type,Amount,Shares/Unit
06/30/2021,VANG SM VAL IDX INST,Exchange In,"1,248.46","29.940"
I had to write code to get rid of the commas inside the quotes because I couldn't use StrReplace().
I created a function to do this, since maybe it's not the last time:
function RemoveCommaInQuotes( String )
{
// Removes unwanted commas inside of quotes, e.g. in a CSV file where the commas are needed so can't use StrReplace().
// Useful for CSV files, e.g. exported Fidelity transactions.
// 20210812
InQuotes = 0;
NewString = "";
for( i = 0; i < StrLen( String ) ; i++ )
{
Char = StrMid( String, i, 1 );
if( Char == "\"" )
{
InQuotes = 1 - InQuotes;
Char = "";
}
if( Char == "," AND InQuotes ) Char = "";
NewString += Char;
}
return NewString;
}
I hope this helps someone.