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

Read CSV File

Contributor ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

Hi,

     I am using the below function to read csv file. Recently noticed this issue. I did not know why its happening.. is it related to eof reason or any other reason?

     When the csv file end of the row is only "F" its not returning by the function. When the empty para or the text para is given to the next line of "F", its returned. Why its happening?? is there is any reason...  How to handle this case?

     CSV :

CSV.png    

ReturnValues.png

  Empty Next Line (or) next line with contents:

Screen Shot 2018-09-27 at 1.19.00 PM.png

  Empty next line:

Screen Shot 2018-09-27 at 1.06.38 PM.png

Next line is bold:

Screen Shot 2018-09-27 at 1.06.59 PM.png

     Code:

function readFiles(filStr,fileFlag,column1,column2,column3,column4,columnArr){

    var readFil = File(filStr);

    var rCnt = 0;

    if(!readFil.exists){

        return false;

    }else{

        var fileContnts = "";

        readFil.open("r");

        if(fileFlag == "TXT"){

            fileContnts = readFil.read();

        }else{

             

            while(readFil.eof==false){

                try{

                    zRead=readFil.readln();

                   

                    var csvR = zRead.split(",");

                    if(rCnt != 0){

                        column1.push(csvR[columnArr[0]]);

                        column2.push(csvR[columnArr[1]]);

                        column3.push(csvR[columnArr[2]]);

                        column4.push(csvR[columnArr[3]]);

                        rCnt++;

                    }   

                }catch(e){}

                rCnt = 1;

            }

        }

        readFil.close();

    }

}

Regards,

Sudha K

TOPICS
Scripting

Views

859

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
Contributor ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

Hi,

Collect the concept from here:

var myFile = File.openDialog("Choose a tab delimitted txt file:"); 

if (!myFile){exit();}      

 

var myArray = []  

myFile.open('r'); 

while (myFile.eof==false){ 

    var line=myFile.readln().split(","); 

    alert(line[0])

    myArray.push(line[0]); 

    }

Selva

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
Community Expert ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

Hi Sudha,

The issue with your code is that you are using readln and since it reads lines demarcated by newline and the last line does not have a new line after it, that line is skipped.

A better way would be to read the whole file into a string and then split it up on new line character, something like mentioned below.

var f = new File("Path to your file")

f.open("r")

var c = f.read()

var a = c.split("\n")     //In case the line ends with a new line character you will have a blank line in the last entry. Check it and purge it

Hope this helps

-Manan

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
Contributor ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

Hi,

     Thank you for your reply... But my doubt is , if I use the same code for the below case its working.  How it is ??

Input CSV:

Screen Shot 2018-09-28 at 11.18.24 AM.png

Result:

     Screen Shot 2018-09-28 at 11.08.50 AM.png

    

     Is there is any reason behind the end character is "F"? Because if i append any character with that without any other changes its returning that row contents.  How it is??

    Screen Shot 2018-09-28 at 11.20.22 AM.png

     If its the reason, it should not return the last row contents know? is it right??

- Sudha K

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
Community Expert ,
Sep 27, 2018 Sep 27, 2018

Copy link to clipboard

Copied

You mean to say this new screenshot that you pasted reads all the lines using readln even if the last line is not terminated by a new line? If so send me the file i can have a look

-Manan

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
Contributor ,
Sep 28, 2018 Sep 28, 2018

Copy link to clipboard

Copied

Yes...  I don't know the reason for that so only posted to know the reason..

I don't know how to attach file here.  I created this file in mas os textEdit.

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
Community Expert ,
Sep 28, 2018 Sep 28, 2018

Copy link to clipboard

Copied

Upload the file on dropbox or gdrive and post the link here.

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
Contributor ,
Sep 28, 2018 Sep 28, 2018

Copy link to clipboard

Copied

When u using this kind of csv, is it working correctly for u?

Can u pls create csv using textedit ..

What is dropbox and post it here.. Can u pls explain the steps?

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
Community Expert ,
Sep 28, 2018 Sep 28, 2018

Copy link to clipboard

Copied

It works as expected and described by me in my first reply. I would like to see your document to understand what you are seeing different. Google dropbox you would find it is

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
Contributor ,
Sep 28, 2018 Sep 28, 2018

Copy link to clipboard

Copied

LATEST

Thank you for your reply..

Currently i could not able to access the url for dropbox.  So i could not able to post the csv file.

Can i upload the file using forum by attach file?

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