Showing posts with label CSV. Show all posts
Showing posts with label CSV. Show all posts

Tuesday, August 24, 2021

how to use Powershell to convert CSV file into Json format data?

 there is a built-in function in powershell cmdlet, which will allow us to convert the csv data into json format data.

first we should go to the folder where is the csv file stored. my csv file is found in this folder

C:\code\django\django-search-app\fixtures

PS C:\code\django\django-search-app\fixtures> ls

    Directory: C:\code\django\django-search-app\fixtures

Mode                 LastWriteTime         Length Name

----                 -------------         ------ ----

-a----         8/24/2021   1:26 PM         872488 books.csv

-a----         8/24/2021   1:35 PM         883612 books_title_author.csv


here is script to be run the in windows powershell and it should be executed inside the  folder

PS C:\code\django\django-search-app\fixtures> $topicsjson = import-csv .\books_title_author.csv | ConvertTo-Json

PS C:\code\django\django-search-app\fixtures> $topicsjson | Add-Content -Path "myData.json"

 $topicsjson = import-csv .\books_title_author.csv | ConvertTo-Json

 $topicsjson | Add-Content -Path "myData.json"

after the scripts is executed.

there should be a json file name myData.json within the file.

PS C:\code\django\django-search-app\fixtures> ls

    Directory: C:\code\django\django-search-app\fixtures

Mode                 LastWriteTime         Length Name

----                 -------------         ------ ----

-a----         8/24/2021   1:26 PM         872488 books.csv

-a----         8/24/2021   1:35 PM         883612 books_title_author.csv

-a----         8/24/2021   1:40 PM        2047343 myData.json





Thursday, September 21, 2017

How to quickly parse the CSV file into an array or Table in C#?

I just receive an csv file that i need to process it and grab email from email column and add the cotent to email message body from the content column.


actually we can handle it in two way, the first one, we can use the TextFieldParser from VisualBasic.FileIO to read the data into the parser then process them.



        List<string[]> parsedData = new List<string[]>();
            string[] fields;

            //string line = parser.ReadLine();

            try
            {
                TextFieldParser parser = new TextFieldParser(@"c:\temp\sample.csv");
                parser.TextFieldType =Microsoft.VisualBasic.FileIO.FieldType.Delimited;
                parser.SetDelimiters(",");

                while (!parser.EndOfData)
                {
                    fields = parser.ReadFields();
                    parsedData.Add(fields);
                }

                parser.Close();

            }
            catch (Exception e)
            {
                //MessageBox.Show(e.Message);
            }



the second one, you can download the CSVHelper from Nuget Package Management Windows




             List<string[]> parsedData = new List<string[]>();
            using (StreamReader reader = File.OpenText(@"c:\temp\sample.csv"))
            {
                var parser = new CsvParser(reader);

                while (true)
                {
                    var row = parser.Read();

                    if (row == null)
                    {
                        break;
                    }
                    else
                    {
                        parsedData.Add(row);
                    }
                }
            }