0

I have a CSV file of songs with information split with a comma and I want to convert them to list of classes (see below the class).

sorry if I don't explain very well

For example:

Waiting For Love,Avicii,Tanecni Liga 171,Dance & House,Avicii,,1,2015,,,,

The problem is that I have some songs with quoting mark and inside the quoting mark there is a comma for example:

Hey Brother,Avicii,TRUE,House,Avicii,,3 of 12,2013,,,"Tim Bergling, Ash Pournouri, Vincent Pontare & Salem Al Fakir",

the problem is that I want that - "Tim Bergling, Ash Pournouri, Vincent Pontare & Salem Al Fakir" will be one string, but if I split with a comma like solutions I found here it will cut in the middle of the string where I don't want to

If I'll use Split(','); on this line, It will do this:

Hey Brother
Avicii
TRUE
House
Avicii
//empty string because there is ',,'
3 of 12
2013
//empty string because there is ',,'
//empty string because there is ',,'
"Tim Bergling
 Ash Pournouri
 Vincent Pontare & Salem Al Fakir"
//empty string because there is ',,'

Instead, I want this:

Hey Brother
Avicii
TRUE
House
Avicii
//empty string because there is ',,'
3 of 12
2013
//empty string because there is ',,'
//empty string because there is ',,'
Tim Bergling, Ash Pournouri, Vincent Pontare & Salem Al Fakir
//empty string because there is ',,'

Here is my song class:

    `public class song
    {
        private string name; // Name of the song
        private string artist; // Name of the artist
        private string album; // The name of the album
        private string genre; // The Genre of the song
        private string album_Artist; // Artist name of the album
        private string release_Date; // The release date of the song
        private string track; // The number of the track of the number of the count of the tracks - a.k 4 of 12
        private string year; // The year that the song came
        private string comments; // The comments for the song
        private string description; // The description for the song
        private string composer; // The composer of the song;
        private string grouping; // The grouping of the song
    }

I have Get and Sets action for every string (I cut them because it's too long)

Raz Luvaton
  • 3,166
  • 4
  • 21
  • 36

1 Answers1

1

Without using any special libraries you can just reference Microsoft.VisualBasic in your C# code and use the TextFieldParser as per the below

(Copied from https://stackoverflow.com/a/3508572/1658434)

using (TextFieldParser parser = new TextFieldParser(@"c:\temp\test.csv"))
{
    parser.TextFieldType = FieldType.Delimited;
    parser.SetDelimiters(",");
    while (!parser.EndOfData) 
    {
        //Processing row
        string[] fields = parser.ReadFields();
        foreach (string field in fields) 
        {
            //TODO: Process field
        }
    }
}

I have tested it using your example and it works just as you need.

Community
  • 1
  • 1
  • Thank you very much!, I will check this later, but how this is defining what inside "" as one string? – Raz Luvaton May 17 '17 at 12:19
  • Well, TextFieldParser, as the name implies, is a parser. So I believe it puts into consideration the rules and format of a delimited file. Which allows a field to be quoted with double quotes, and therefore, can identify it as such. – Ghadah Fakhry May 17 '17 at 12:31