如何在C#中写入CSV时忽略字符串中的特殊字符

发布于 2025-01-24 11:46:14 字数 784 浏览 2 评论 0原文

我正在使用StreamWriter在CSV Result.message- 中写入记录,

 Data = "abcd+ " , " + "Error + " , " + result.Message;
 using (StreamWriter streamWriter = new StreamWriter("c:/temp/data.csv"))
    {
      streamWriter.WriteLine(Data);
    }

无法对来自encompass的有序服务进行轮询:{\ r \ n \“ code \”:\“ vpc -9999999 \”,\ r \ r \ n \'\ r \ n \“”摘要\“:\”错误在供应商平台\“,\ r \ n \”详细信息\“:\”伙伴错误发生 - e206:访问报告拒绝\“,\ r \ n \” product \“ product \”:e

因此它具有特殊的字符。

在CSV文件中,我需要有3列,其中

1st coulmn -ABCD

第二列 - 错误

3rd列 - 失败了从encompass到订购服务的投票:{\ r \ r \ n \ code \“ code \”:\“ vpc-999999 \” “,\ r \ n \”摘要\“:\”错误在供应商平台\“,\ r \ n \”详细信息\ \“:\”伙伴错误发生 - n \“ product \”:e

由于该长文本包含特殊字符,因此在CSV中,它将写入下一行等。如何将整个消息视为1个文本并将其放在1个单元格中?

I am using StreamWriter to write records in csv

 Data = "abcd+ " , " + "Error + " , " + result.Message;
 using (StreamWriter streamWriter = new StreamWriter("c:/temp/data.csv"))
    {
      streamWriter.WriteLine(Data);
    }

Result.Message - Failed to poll for ordered service from Encompass: {\r\n \"code\": \"VPC-999999\",\r\n \"summary\": \"Error occurred in Vendor Platform\",\r\n \"details\": \"Partner error occurred - E206 : Access to report denied\",\r\n \"product\": e

So it has special characters in it.

In csv file I need to have 3 columns with

1st coulmn - abcd

2nd column - Error

3rd column - Failed to poll for ordered service from Encompass: {\r\n \"code\": \"VPC-999999\",\r\n \"summary\": \"Error occurred in Vendor Platform\",\r\n \"details\": \"Partner error occurred - E206 : Access to report denied\",\r\n \"product\": e

Since this long text contains special chars, in csv it gets written to next line and so. How to treat this whole message as 1 text and can put it in 1 cell?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平安喜乐 2025-01-31 11:46:14

问题的一部分是,“ CSV格式”的定义很差……至少可以说。 Microsoft Excel实施可能与您所获得的“参考实现”可能与“参考实现”一样接近,并且在其中有一些奇怪的角落。还有 rfc 4180 定义了CSV数据的ABNF Grammar。

但是,我不会使用自己的CSV工具,而不是自己滚动。

  • sebastien lorion的快速CSV读取器 ,但仅是读者。

  • fastcsv ,看起来很有趣,但我不能说我用过了。

  • Josh Close的 csvhelper 看起来也不错,但是再次,我还没有使用过。 /p>

我敢肯定还有其他。

A part of the problem is that the "CSV format" is... poorly defined... to say the least. The Microsoft Excel implementation is probably as close to a "reference implementation" as you'll get and it's got some weird corners in it. There is also RFC 4180 that defines a ABNF grammar for CSV data.

But rather than rolling your own, I would use CSV tooling that has had the kinks worked out of it.

  • Sebastien Lorion's Fast CSV Reader is a good tool, but a reader only.

  • FastCSV, looks interesting, but I can't say I've used it.

  • Josh Close's CsvHelper also looks good, but again, I haven't used it.

I'm sure there are others as well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文