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

Read the CSV file multiple columns using for loop

Contributor ,
Aug 16, 2021 Aug 16, 2021

Hi All,

I'm using the below functions to read the CSV file, I have able to read the rows using loop and my requirement is need to loop the columns, in my CSV I have around 50-60 columns and need to fetch the data each columns wise instead of hard code the column number is it possible?

    GettingInputFromCSV_UI(File(AssetCSV_File.fullName));
    if(Replacement_Content_csv.length>0){//Replacement_Content_csv // Rules_styleMapping_csv
        for(var a=0;a<Replacement_Content_csv.length;a++){
            FolderNames.push(String(Replacement_Content_csv[a][0]));
            SubFolderNames.push(String(Replacement_Content_csv[a][1]));
            if(Rules_styleMapping_head_csv.length>0){
                for(var b=0;b<Rules_styleMapping_head_csv.length;b++){
                    if(String(Rules_styleMapping_head_csv[b]) != undefined && String(Rules_styleMapping_head_csv[b]) != ""){
                        if(String(Rules_styleMapping_head_csv[b]) == "Test1") {
                            ReadCSV_ColumnsTHTH.push(Replacement_Content_csv[a][2]);
                        }
                        if(String(Rules_styleMapping_head_csv[b]) == "Test2") {
                            ReadCSV_ColumnsUSEN.push(Replacement_Content_csv[a][3]);
                        }
                        if(String(Rules_styleMapping_head_csv[b]) == "Test3") {
                            ReadCSV_ColumnsWWEN.push(Replacement_Content_csv[a][4]);
                        }
                    }
                }
            }
        }
    }

function GettingInputFromCSV_UI(fs){
	var csvFLG = 0;
    var LineCount = 0;
	fs.open("r");
	zRead =fs.readln();		//heading
    var HeadCommareplace = String(zRead).replace(", ","<COMMA>","g");
    Rules_styleMapping_head_csv = HeadCommareplace.split(",");
    ReadCSV_ColumnsHeads.push(Rules_styleMapping_head_csv);

	while(fs.eof == false){
		zRead =fs.readln();
		if(zRead.length != 0){
            zRead = zRead.replace(", ","<COMMA>","g");
			var LineArr = zRead.split(",");
			var LineStr = "";
            LineStr = LineArr.join("");
			if(LineStr != ""){
                Replacement_Content_csv.push(LineArr);
			}
		}
	}
	fs.close();
}

 

TOPICS
Scripting
308
Translate
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
Community Expert ,
Aug 17, 2021 Aug 17, 2021
LATEST

You already know the column count at various stages, e.g. Rules_styleMapping_head_csv.

 

Your code seems over-elaborate. Instead of this:

var LineStr = "";
LineStr = LineArr.join("");
if(LineStr != ""){

(to check whether a string has any commas) you could use this:

if (LineArr.length > 1)

Or better yet, replace this:

var LineArr = zRead.split(",");
var LineStr = "";
LineStr = LineArr.join("");
if(LineStr != ""){
  Replacement_Content_csv.push(LineArr);
}

with this:

if(zRead.indexOf(',') > -1){
  Replacement_Content_csv.push(zRead.split(","));
}

P.

Translate
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