Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to convert a list of objects to csv with each object values in a new line

Tags:

c#

csv

I have a class as follows :

public class Test {     public int Id {get;set;}     public string Name { get; set; }     public string CreatedDate {get;set;}     public string DueDate { get; set; }      public string ReferenceNo { get; set; }            public string Parent { get; set; } } 

and I have a list of Test objects

List<Test>testobjs=new List(); 

Now I would like to convert it into csv in following format:

"1,John Grisham,9/5/2014,9/5/2014,1356,0\n2,Stephen King,9/3/2014,9/9/2014,1367,0\n3,The Rainmaker,4/9/2014,18/9/2014,1";

I searched for "Converting list to csv c#" and I got solutions as follows:

string.Join(",", list.Select(n => n.ToString()).ToArray()) 

But this will not put the \n as needed i.e for each object

Is there any fastest way other than string building to do this? Please help...

like image 813
PassionateProgrammer Avatar asked Sep 05 '14 09:09

PassionateProgrammer


People also ask

How do you convert an object to a list of objects?

toList()) can be used to collect Stream elements into a list. We can leverage this to convert object to a list. //Similarly, you can change the method in the map to convert to the datatype you need.

How do I write a list of CSV files in Python using pandas?

CSV: Import the csv module in Python, create a csv writer object, and write the list of lists to the file in using the writerows() method on the writer object. Pandas: Import the pandas library, create a Pandas DataFrame, and write the DataFrame to a file using the DataFrame method DataFrame. to_csv('file. csv') .

How do I save a CSV list in pandas?

Using Pandas to_csv() function To convert the list to csv, we need to convert from list to dataframe and then use the to_csv() function to convert dataframe to a csv file. In this example, we have first imported pandas library and then define the four lists and map it with its column using a dictionary.


2 Answers

Use servicestack.text

Install-Package ServiceStack.Text 

and then use the string extension methods ToCsv(T)/FromCsv()

Examples: https://github.com/ServiceStack/ServiceStack.Text

Update: Servicestack.Text is now free also in v4 which used to be commercial. No need to specify the version anymore! Happy serializing!

like image 156
unreal Avatar answered Oct 06 '22 11:10

unreal


Because speed was mentioned in the question, my interest was piqued on just what the relative performances might be, and just how fast I could get it.

I know that StringBuilder was excluded, but it still felt like probably the fastest, and StreamWriter has of course the advantage of writing to either a MemoryStream or directly to a file, which makes it versatile.

So I knocked up a quick test.

I built a list half a million objects identical to yours.

Then I serialized with CsvSerializer, and with two hand-rolled tight versions, one using a StreamWriter to a MemoryStream and the other using a StringBuilder.

The hand rolled code was coded to cope with quotes but nothing more sophisticated. This code was pretty tight with the minimum I could manage of intermediate strings, no concatenation... but not production and certainly no points for style or flexibility.

But the output was identical in all three methods.

The timings were interesting:

Serializing half a million objects, five runs with each method, all times to the nearest whole mS:

StringBuilder     703     734     828     671     718   Avge=     730.8 MemoryStream      812     937     874     890     906   Avge=     883.8 CsvSerializer   1,734   1,469   1,719   1,593   1,578   Avge=   1,618.6 

This was on a high end i7 with plenty of RAM.

Other things being equal, I would always use the library.

But if a 2:1 performance difference became critical, or if RAM or other issues turned out to exaggerate the difference on a larger dataset, or if the data were arriving in chunks and was to be sent straight to disk, I might just be tempted...

Just in case anyone's interested, the core of the code (for the StringBuilder version) was

    private void writeProperty(StringBuilder sb, string value, bool first, bool last)     {         if (! value.Contains('\"'))         {             if (!first)                 sb.Append(',');              sb.Append(value);              if (last)                 sb.AppendLine();         }         else         {             if (!first)                 sb.Append(",\"");             else                 sb.Append('\"');              sb.Append(value.Replace("\"", "\"\""));              if (last)                 sb.AppendLine("\"");             else                 sb.Append('\"');         }     }      private void writeItem(StringBuilder sb, Test item)     {         writeProperty(sb, item.Id.ToString(), true, false);         writeProperty(sb, item.Name, false, false);         writeProperty(sb, item.CreatedDate, false, false);         writeProperty(sb, item.DueDate, false, false);         writeProperty(sb, item.ReferenceNo, false, false);         writeProperty(sb, item.Parent, false, true);     } 
like image 22
PolicyWatcher Avatar answered Oct 06 '22 11:10

PolicyWatcher