c#系统IO,读取文件
本文关键字:读取 文件 IO 系统 | 更新日期: 2023-09-27 18:03:22
我在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;""> </span></p> <p class=""MsoNormalCxSpMiddle"" style=""margin: 0cm 0cm 0pt;""><span style=""font-family: Arial; font-size: small;""> </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"
我需要写一段代码,当你看到一个"(一个数字),然后在文件中创建一个新的行,否则追加到前一行。
我需要创建一个webform应用程序或控制台应用程序,这并不重要。
您需要的是一个支持引号字符串的正确CSV解析器。这个问题可能有答案…用c#解析CSV文件,带有头文件
如果您确实不能使用外部代码,请尝试您在注释中提供的代码的修改版本:
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();
是的,伙计们,我想我们已经成功了。谢谢你
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);
谢谢大家Much appreciate