-1

I have a file in textpad:

"ID","Product Code","Supplier Code","Description","SEO Page Title","SEO Meta Description","SEO Meta Keywords","On-Page Name","On-Page Description","On-Page Features","Smart URL"
"1","301897","N/A","Brother DR7000 Drum","Brother DR7000 Drum",,"Brother Drum","Brother DR7000 Drum",,,"301897-brother-drum"
"2","300021","N/A","Post-It Index Arrows 12mm 684-ARR4","Post It Index Arrows 12mm 684 ARR4",,"Post It Index Arrows ","Post-It Index Arrows 12mm 684-ARR4",,,"300021-post-it-index-arrows"
"8558","SMP26","N/A","Shoreline SMP26 Portable Pharmacy Refrigerator","Shoreline SMP26 Portable Pharmacy Refrigerator",,,"Shoreline SMP26 Portable Pharmacy Refrigerator","·         12 Volt DC (fitted with Car cigarette lighter connection for mobile operation) or 220/110 Volt AC (fitted with 3 pin plug for mains power operation)
·         385mmH x 345mmW x 510mm/26 litres Capacity/33 watts
·          RPSGB, NHS, WHO, RCVS compliant 
·         Digital LED Temperature Display/Audio / Visual Temperature Alarm
385mmH x 345mmW x 510mm/26 litres Capacity/33 watts
RPSGB, NHS, WHO, RCVS compliant 
Digital LED Temperature Display/Audio / Visual Temperature Alarm
2 Years Parts & Labour Warranty (UK only)
","smp26-shoreline-smp26-portable-pharmacy-refrigerator"
"8559","SMP41","N/A","Shoreline SMP41 Portable Pharmacy Refrigerator","Shoreline SMP41 Portable Pharmacy Refrigerator",,,"Shoreline SMP41 Portable Pharmacy Refrigerator","<p class=""MsoNormalCxSpMiddle"" style=""text-align: right; line-height: normal; margin: 0cm 21.25pt 0pt 0cm; mso-add-space: auto;"" align=""right""><span style=""font-family: Arial; font-size: small;"">&nbsp;</span></p>&#13;&#10;<p class=""MsoNormalCxSpMiddle"" style=""margin: 0cm 0cm 0pt;""><span style=""font-family: Arial; font-size: small;"">&nbsp;</span></p>","12 Volt DC (fitted with Car cigarette lighter connection for mobile operation) or 220/110 Volt AC (fitted with 3 pin plug for mains power operation)
400mmH x 610mmW x 385mmD/41 litres Capacity
Temperature Range +2°C to +8°C/Forecd air cooling/Digital LED temperature display/Audio Visual temperature alarm
Suitable for Vaccine & Pharmaceutical Storage 
RPSGB, NHS, WHO, RCVS compliant 
2 Years Parts & Labour Warranty (UK only)
","smp41-shoreline-smp41-portable-pharmacy-refrigerator"

This file has carriage returns and loads of crap data in it. I basically need to do the following before loading into a database. I need to read the file do something with the values in the text file and then output the new file.

If you look above, Lines:

"1","301897","N/A","Brother DR7000 Drum","Brother DR7000 Drum",,"Brother Drum","Brother DR7000 Drum",,,"301897-brother-drum"
"2","300021","N/A","Post-It Index Arrows 12mm 684-ARR4","Post It Index Arrows 12mm 684 ARR4",,"Post It Index Arrows ","Post-It Index Arrows 12mm 684-ARR4",,,"300021-post-it-index-arrows"

are correct.

However line:

"8558","SMP26","N/A","Shoreline SMP26 Portable Pharmacy Refrigerator","Shoreline SMP26 Portable Pharmacy Refrigerator",,,"Shoreline SMP26 Portable Pharmacy Refrigerator","·         12 Volt DC (fitted with Car cigarette lighter connection for mobile operation) or 220/110 Volt AC (fitted with 3 pin plug for mains power operation)
·         385mmH x 345mmW x 510mm/26 litres Capacity/33 watts
·          RPSGB, NHS, WHO, RCVS compliant 
·         Digital LED Temperature Display/Audio / Visual Temperature Alarm
385mmH x 345mmW x 510mm/26 litres Capacity/33 watts
RPSGB, NHS, WHO, RCVS compliant 
Digital LED Temperature Display/Audio / Visual Temperature Alarm
2 Years Parts & Labour Warranty (UK only)
","smp26-shoreline-smp26-portable-pharmacy-refrigerator"

which is broken,

I need to write a bit of code that says when you see a "(a number) then create a new line in the file, else append to the previous line.

I need to create a webform app or console app it doesn't matter.

Steven
  • 91
  • 1
  • 9

2 Answers2

2

What you want is a proper CSV parser that supports quoted strings. This question might have the answer ... Parsing CSV files in C#, with header.

If you really can't use external code, try this modified version of the code you provided in comments:

StreamReader reader = new StreamReader("input.txt");
List<string> result = new List<string>();
string new_line = reader.ReadLine();
string full_line = null;
while (new_line != null)
{
    // concatenate input when full_line not complete
    full_line = (full_line == null) ? new_line : full_line + new_line;
    // check for expected line completion pattern
    // looking for a " that is not escaped \" ... adapt this to your input assumptions
    if (new_line.EndsWith("\"") && !new_line.EndsWith("\\\""))
    {
        result.Add(full_line);
        full_line = null;
    }
    new_line = reader.ReadLine();
} 
reader.Close(); 
Community
  • 1
  • 1
zeFrenchy
  • 6,541
  • 1
  • 27
  • 36
  • Currently I have working and outputing to the console. _italic_ class Program { static void Main(string[] args) { StreamReader objReader = new StreamReader("C:\\Data\\test.txt"); string sLine = ""; ArrayList arrText = new ArrayList(); while (sLine != null) { sLine = objReader.ReadLine(); if (sLine != null) arrText.Add(sLine); } objReader.Close(); foreach (string sOutput in arrText) Console.WriteLine(sOutput); Console.ReadLine(); _italic_ – Steven Mar 11 '13 at 10:35
  • You will need something a little more clever that than to support quoted strings in you input CSV. Look into the linked question for canned solutions. Do you need to preserve the spurious newlines? If not, you might be able to do upfront text substitution to get out of trouble. – zeFrenchy Mar 11 '13 at 10:46
  • Sorry I am new to c#. The code Im using above when stepping through the code the value looks at "\" first. "\"1\",\"301897\",\"N/A\",\"Brother DR7000 Drum\",\"Brother DR7000 Drum\",,\"Brother Drum\",\"Brother DR7000 Drum\",,,\"301897-brother-drum\"" string Can I not write something in the while statement, if sLine starts with the value ="\"(number) then write to file else continue – Steven Mar 11 '13 at 11:16
  • @Chris, ah i see, thanks. I am using StreamReader to write to console. In the while statment is it possible to create a array variable and give it a value of all the 9 numbers. Then have something to say if the StreamReader value begins with the array variable(the 9 numbers). Eg, like in T-Sql you can say in the where clause where not IN (1,2,3,4,5,6,7,8,9) – Steven Mar 11 '13 at 11:57
  • @Dominique Jacquel I think the code above might work. How do I write to file? Do I have to change the StreamReader to StreamWriter. – Steven Mar 11 '13 at 12:51
  • I'm pretty sure it does work if the input assumptions are correct. – zeFrenchy Mar 11 '13 at 13:03
  • Oh and it's one question at a time. – zeFrenchy Mar 11 '13 at 13:03
  • it looks like its working, Thanks. Until I can write to file I can not see the file as the console application doesn't allow me to copy and paste. Just googling on how to write the result to file and ill let you know – Steven Mar 11 '13 at 13:06
  • if (newLine.EndsWith("\"") && !newLine.EndsWith("\\\"")) Does this mean looks at \" and not \\\" then it looks for the next line? Going through the code on vs2010 I dont see how this is working, i can not see this condition working but it seems to work. although no variable value looks to have \\\" contained within. Pulling my hair out with this as I cant get this to write to file to have a look at it either. Copy and paste the console (cmd) values doesn't work as well as I hoped, doesn't show in one whole row but copys and pastes like a bloke in the console app. – Steven Mar 11 '13 at 14:07
  • Thanks guys, still dont know how if it checks for \\\" at the end of each line how it has worked, as no line has \\\" ending it. Is this a vs2010 thing? – Steven Mar 11 '13 at 14:45
  • A " within a quoted string may be represented as \" or "" depending on the convention you use. I assumed you'd use \" and "\\"" is simply representing \" as a C# string. – zeFrenchy Mar 11 '13 at 15:15
0

Yes guys, i think we got it working. Thank you

StreamReader reader = new StreamReader(@"C:\test.txt");
StreamWriter writer = new StreamWriter(@"C:\test2.txt", true);

List<string> result = new List<string>();
string new_line = reader.ReadLine();
string full_line = null;

while (new_line != null)
{
    full_line = (full_line == null) ? new_line : full_line + new_line;

    if (new_line.EndsWith("\"") && !new_line.EndsWith("\\\""))
    {
        //Write to the List
        result.Add(full_line);
        //Reset the whole line
        full_line = null;
    }

    //Read the next line
    new_line = reader.ReadLine();
}

//Close the connection
reader.Close();

foreach (string readResult in result)
    writer.WriteLine(readResult);

Thank You All Much Appreciated

Chris Moutray
  • 18,029
  • 7
  • 45
  • 66
Steven
  • 91
  • 1
  • 9
  • 1
    Are you answering your own question using pretty much the code I answered with earlier? And without accepting the original answer. That could be considered a little rude. – zeFrenchy Mar 11 '13 at 15:24
  • Sorry, I did put it up as a answer because it didn't fit into the comment pane. Also I voted for your answer before I put mine up. Didn't mean to be rude, Thanks again for your help. – Steven Mar 12 '13 at 08:35