Copy link to clipboard
Copied
Hello,
I have a tab-delimited .txt file which I have to import into Indesign for further processing.
The file is composed by a 3 columns header row at the beginning (Code, Description, price) followed by a series of 3 columns data rows.
The problem is that sometimes, depending on the way the txt/csv file has been created, may include unwanted characters (such as spaces, double spaces, etc.).
Is there a way to "clean" the imported strings from these unwanted characters?
This is my starting code:
function processImportedTxt(){
//Open .csv file
var csvFile = File.openDialog("Open file .csv","tab-delimited(*.csv):*.csv;");
datafile = new File(csvFile);
if (datafile.exists){
datafile.open('r');
};
var csvData = new Array();
while(!datafile.eof){//read every row till the end of file
csvData.push(datafile.readln());
}
datafile.close();
for(a=1; a<csvData.length; a++){
var myRowData = csvData;//row of data
var mySplitData = myRowData.toString().split("\t");//divide columns
var myRowCode = mySplitData[0];
var myRowDesc = mySplitData[1];
var myRowPrice = mySplitData[2];
// Here goes code for cleaning strings from unwanted characters
}
}
processImportedTxt();
Any help would be much appreciated
Thanks in advance
Copy link to clipboard
Copied
somestring.replace(/\s+/g,'') //\s = wildcart for any space (including tab)
Copy link to clipboard
Copied
Thanks. Sorry: what "/g" stands for?
Copy link to clipboard
Copied
get every occurence, not just the first ...
Copy link to clipboard
Copied
Great! Could you please point me to some source where I can find something more about these wildcarts?
Copy link to clipboard
Copied
Hi,
If you want to safe 1-space occurences just a small correction:
i.e.:
var myRowCode = mySplitData[0].replace(/\s\s+/g,'');
Jarek