如何使用Javascript处理CSV中的逗号

如何使用Javascript处理CSV中的逗号

本文介绍了如何使用Javascript处理CSV中的逗号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果数据中包含,",导出到csv时会遇到一些问题.

I am having some data which is facing issue while exporting to csv if the data contains "," with in it.

这里是代码:

  CSVData.push('"' + item.OrgId+ '","' + item.Name + '","' + item.OrgDescriptionString + '","' + itam.OrgDetailsString + '","' + item.Active+ '"');

如果orgDetaisl字符串中包含,",则数据将重叠并移至下一列.

if orgDetaisl string contains "," with in it, the data is getting overlapped and moving to next column.

如何转义,".我尝试使用双引号"",但不确定如何实现.

How to escape "," .I tried using double quotes """ but not sure how can i achieve.

CSVData.push('"' + item.OrgId+ '","' + item.Name + '","\""' + item.OrgDescriptionString + '"\"","\""' + item.OrgDetailsString + ' \"","' + item.Active+ '"');

请更正字符串,并帮助我在数据中转义为.".

Please correct the string and help me to escape "," with in the data.

推荐答案

在字段内转义分隔符的正确方法是将字段括在双引号中

The proper way to escape separators inside fields is to enclose the fields in double quotes:

Id,Description
123,"Monitor, Samsung"

但是随后必须用另一个引号将字段内的引号转义:

But then quotes inside the field have to be escaped with another quote:

Id,Description
123,"Monitor 24"", Samsung"

当然,可以使用其他分隔符,例如; ,但是您仍然必须检查那些分隔符是否出现在数据中.

Of course, a different delimiter like ; could be used, but then you'd still have to check if those appear in the data.

这篇关于如何使用Javascript处理CSV中的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 05:04