Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I have text file format like below. I am saving into csv file in c#.
//need to ignore Personnel Clearance Pair line,Credential line in the text file.
Access.text file:
Name:,4th Floor, IT
Personnel Clearance Pair
Clearance name:,iT
Credential
Card number:,1234

Name:,Graham, John
Personnel Clearance Pair
Clearance name:,Temps
Credential
Card number:,23489

I am looping throught the text file like below
try
 {
System.IO.TextWriter Summarytw = System.IO.File.CreateText(sOutputFilePath + sOutFileName_DST);

//Here I am adding column names to csv file(output file).
                string line1 = string.Empty;
                string strColumnname = string.Empty;//{"Name","Clearance name","Card number"};
                strColumnname = "Name,Clearance name,Card number";
                string[] arr = strColumnname.Split(',');
                int a = arr.Length;

                for (int y = 0; y < arr.Length; y++)
                {
                    line += arr[y].ToString() + ",";
                }
                Summarytw.WriteLine(line);

//here I am reading the text file(input file)
  using (System.IO.StreamReader sr = new System.IO.StreamReader(sInputPath + sInFile))
                {

                    while ((line = sr.ReadLine()) != null)
                    {
//I need to save only name, clearance name, card number values to the csv file.
 if ((line.Contains("Name")) || (line.Contains("Clearance name")) || (line.Contains("Card number")))
                        {
                            string s = line.ToString();
                            string pattern = ":,";
                            string strdelimiter = ",";
                            string[] substrings = Regex.Split(line, pattern);
                            for (int i = 1; i < substrings.Length; i++)
                            {
                                string value = substrings[1].ToString();
                                if (i == substrings.Length)
                                {
                                    strconcat += " " + value + " ";
                                }
                                else
                                {
                                    strconcat += "\" " + value + "\"";
                                    strconcat += strdelimiter;
                                }

                            }


                        }


                        if (line.StartsWith("Card number"))
                        {
                            strconcat += Environment.NewLine;
                        }


                    }

                    Summarytw.WriteLine(strconcat);
                }
}


for the above line in Access.txt code it was working fine.

but if the data is like below

Name:,lori, Julie
Personnel Clearance Pair
Clearance name:,Access Full Time Employees //No card number row
Credential 

Name:,Badge 43511, Visitor
Personnel Clearance Pair//No clearance name row
Credential
Card number:,43511


if there is no card number or clearance name the csv file is looking like below.
ACCESS.CSV:
Name Clearance name card number
4th Floor, IT iT 1234
Graham, John Temps 23489
lori, Julie Access (if no card number it is placing with next record details)
Badge 56892 56892 (if clearance name is empty then card number Is saving into clearance name field in csv file.)

The text file data is line by line .

How to read and save it to csv file

What I have tried:

Tried many things but failed.
searched in google but didn't find proper answer.
Please help me.
Thanks inadvance.
Posted
Updated 13-Aug-18 13:20pm
Comments
j snooze 13-Aug-18 17:14pm    
Well, if there is always a name you could just check to see if the current line you are looking at is a name...if it is concat Environment.Newline BEFORE adding name and subsequent fields next comma delimited line. That way you don't have to care if there isn't a nonexistent field after that.

If it matters you can check to see if its the first record and not add a new line before the name the first time around.

1 solution

But in case there is always a name you might check to determine whether the present line you are checking at is a name. . .if it is concat Environment.Newline previous to adding name and following fields alongside comma delimited line. That way you never have to care when there isn't a nonexistent discipline after that. In case it matters you can check to determine whether its the initial listing and perhaps not add a brand new line until the name the very first time around.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900