• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Removing unwanted characters from imported string

Explorer ,
May 09, 2013 May 09, 2013

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

TOPICS
Scripting

Views

818

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 09, 2013 May 09, 2013

Copy link to clipboard

Copied

somestring.replace(/\s+/g,'') //\s = wildcart for any space (including tab)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 09, 2013 May 09, 2013

Copy link to clipboard

Copied

Thanks. Sorry: what "/g" stands for?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 09, 2013 May 09, 2013

Copy link to clipboard

Copied

get every occurence, not just the first ...

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 09, 2013 May 09, 2013

Copy link to clipboard

Copied

LATEST

Great! Could you please point me to some source where I can find something more about these wildcarts?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
May 09, 2013 May 09, 2013

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines