-1

テキストパッドにファイルがあります:

"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"

このファイルには、改行とがらくたデータのロードが含まれています。データベースにロードする前に、基本的に次のことを行う必要があります。ファイルを読み込んでテキスト ファイルの値を処理し、新しいファイルを出力する必要があります。

上を見ると、行:

"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"

壊れているもの、

"(数値) が表示されたらファイルに新しい行を作成し、それ以外の場合は前の行に追加するというコードを少し書く必要があります。

Web フォーム アプリまたはコンソール アプリを作成する必要がありますが、それは問題ではありません。

4

2 に答える 2

2

必要なのは、引用符で囲まれた文字列をサポートする適切な CSV パーサーです。この質問には答えがあるかもしれません... Parsing CSV files in C#, with header .

本当に外部コードを使用できない場合は、コメントで提供した次の修正版のコードを試してください。

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(); 
于 2013-03-11T10:12:22.267 に答える
0

はい、皆さん、うまくいったと思います。ありがとうございました

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);

ありがとうございました

于 2013-03-11T14:28:08.440 に答える