How to write a value that contains a comma to a csv file?

﹥>﹥吖頭↗ 提交于 2021-02-05 08:50:55

问题


I have a javascript program to write values to a csv file. I have a value that includes comma within. How to do this? I am newbie to javascript. can you please help me?

I tried double quotes

content += record.number +","+ record.category +","+ record.status +","+ record.approval_status +","+ record.requested_by +","+ record.assigned_to +","+record.assign_dept +","+ record.coordinator +","+ record.coord_phone +","+ record.planned_start +","+ record.planned_end +","+record.reason +","+ record.risk_assessment +","+ record.logical_name +","+ "record.assets" +","+ record.request_date +","+ record.subcategory +","+ record.priority_code +","+ record.impact +","+  record.sched_outage_start +","+ record.sched_outage_end  + "\n";

record.assets is the field that will have comma within.

Thanks, Chithra


回答1:


Close them in quotes and escape double quotes?

http://en.wikipedia.org/wiki/Comma-separated_values

1999,Chevy,"Venture ""Extended Edition""",4900.00

in your case:

"," + "record.assets" + ","

should be

"," + '"' + record.assets.replace(/"/g,'""') + '"' + ","



回答2:


You can put your text between quotes.
You are not obliged to use a comma , you can use a ";" too (dutch/german format but works well)
--> http://en.wikipedia.org/wiki/Comma-separated_values
1997;Ford;E350;2,34



来源:https://stackoverflow.com/questions/22144184/how-to-write-a-value-that-contains-a-comma-to-a-csv-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!