18 January 2026:


This forum is now archived and is in read-only mode. Please continue discussions on our improved new Sahi Pro Community forum.



Sahi Pro is an enterprise grade test automation platform which can automate web, mobile, API, windows and java based applications and SAP.

writing newlines to csv file

Bluepma38Bluepma38 Members
edited November -1 in Sahi - Open Source
So, I've been working on writing some test results to csv files today, but I've come across an issue of not being able to get newlines into the csv fields. I've tried:

'\n'
"\n"
"\r\n"
chr(10)
chr(13) + chr(10)
String.fromCharCode(10))
String.fromCharCode(13,10))

but no matter what I do, i just end up with literal \n's and \r's in my string within the csv.

I'm thinking that the _writeCSVFile is forcing them to be literal, but was hoping someone might have a solution for me. Thanks.

Comments

  • JaoBrJaoBr Members
    All you have to do is to manipulate the array.

    Something like this:

    array[n] = ["abc"];
    array[n+1] = ["abcd"];

    Or:

    $csv[$csv.length] = ["abc"];
    $csv[$csv.length] = ["abcd"];

    Then you just need to write the array in the csv file.
  • JaoBr wrote:
    All you have to do is to manipulate the array.

    Something like this:

    array[n] = ["abc"];
    array[n+1] = ["abcd"];

    Or:

    $csv[$csv.length] = ["abc"];
    $csv[$csv.length] = ["abcd"];

    Then you just need to write the array in the csv file.

    Not sure you read my question... but I'm looking for a way to insert newlines into strings. For example, If i have a string:

    "this is a really long string that will not look formatted well in a csv file, it really needs to be word wrapped to it can fit correctly within an excel sheet cell."

    I need to be able to insert newlines between words to have this string display on multiple lines within one cell. I'm currently using my wordwrap function which works great at putting "\n" in where I want them, but the csv isn't recognizing the escape sequence and instead is displaying:

    "this is a really long string that \nwill not look formatted well in a \ncsv file, it really needs to be \nword wrapped to it can \nfit correctly within an excel sheet \ncell."

    Like I said above, i've tried a few different ways but none seem to work correctly.
  • narayannarayan Administrators
    Hi Bluepma38,

    The new line can mess up the csv file itself, so it is escaped.
    If you read it back using Sahi, it should give you a String properly with newlines.
    Are you reading it using some other software?

    Regards,
    Narayan
  • Bluepma38Bluepma38 Members
    edited April 2010
    I'm using the csv to log my test results, and then I save them for others to look at, mostly using excel. I'm just trying to format the cells better so it's more readable when they go to check the test results in excel or any other program they use to open the csv in.
  • JaoBrJaoBr Members
    Oh, sorry I missunderstood...
    I already tried to do something like this (with the same objective) but I gave up.

    Narayan, I have a question about CSV files, but not sure if I need to open a new thread...

    Everytime that the information that I save into the CSV file contains characteres like "ç" or "ã", but in the CSV it shows "?" instead of the right char. How can I fix this?
Sign In or Register to comment.