如何解析CSV数据?
在哪里可以找到一些 JavaScript 代码来解析 CSV 数据?
Where could I find some JavaScript code to parse CSV data?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
在哪里可以找到一些 JavaScript 代码来解析 CSV 数据?
Where could I find some JavaScript code to parse CSV data?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(15)
只需使用
.split(',')
:Just use
.split(',')
:编辑为接受换行符作为
\r
\n
和\r\n
作为单字节或双字节字符代码...不能找到了明确的答案,所以我就同意了。Edited to accept newlines as
\r
\n
and\r\n
as either a single or double byte character code ... couldn't find a definitive answer on that so I rolled with it.我构建了这个 JavaScript 脚本来将字符串中的 CSV 解析为数组对象。我发现最好将整个 CSV 分解为行、字段并相应地处理它们。我认为这将使您可以轻松更改代码以满足您的需要。
I have constructed this JavaScript script to parse a CSV in string to array object. I find it better to break down the whole CSV into lines, fields and process them accordingly. I think that it will make it easy for you to change the code to suit your need.
Edit1(2024)
现在 deno std 托管在 https://jsr.io/ ,可以从 esm.sh 使用它
原始答案
我个人喜欢使用 deno std 库,因为大多数模块都与浏览器正式兼容
问题是 std 在打字稿中,但官方解决方案可能会发生将来 https://github.com/denoland/deno_std/issues/641 https://github.com/denoland/dotland/issues/1728
目前有是一个积极维护的动态转译器 https://bundle.deno.dev/
因此您可以使用它就像这样
,你也可以直接供应它
Edit1(2024)
Now deno std is hosted on https://jsr.io/, its possible to use it from esm.sh
Original Answer
Personally I like to use deno std library since most modules are officially compatible with the browser
The problem is that the std is in typescript but official solution might happen in the future https://github.com/denoland/deno_std/issues/641 https://github.com/denoland/dotland/issues/1728
For now there is an actively maintained on the fly transpiler https://bundle.deno.dev/
so you can use it simply like this
and you can also just vendor it
只是把这个扔在那里......我最近遇到需要用 Javascript 解析 CSV 列,我选择了我自己的简单解决方案。它适合我的需要,并且可能对其他人有帮助。
Just throwing this out there.. I recently ran into the need to parse CSV columns with Javascript, and I opted for my own simple solution. It works for my needs, and may help someone else.
正则表达式来救援!这几行代码根据 RFC 4180 标准处理带有嵌入逗号、引号和换行符的正确引用字段。
您不需要解析器生成器,例如 lex/yacc。由于正向查找、负向查找和正向查找,正则表达式可以正确处理 RFC 4180。
克隆/下载代码 https://github.com/peterthoeny/parse-csv-js
Regular expressions to the rescue! These few lines of code handle properly quoted fields with embedded commas, quotes, and newlines based on the RFC 4180 standard.
You don't need a parser-generator such as lex/yacc. The regular expression handles RFC 4180 properly thanks to positive lookbehind, negative lookbehind, and positive lookahead.
Clone/download code at https://github.com/peterthoeny/parse-csv-js
我不知道为什么我无法获取 Kirtan 的示例< /a> 为我工作。它似乎在空字段或带有尾随逗号的字段上失败了......
这个似乎可以处理这两个问题。
我没有编写解析器代码,只是对解析器函数进行了包装以使其适用于文件。参见归属。
I'm not sure why I couldn't get Kirtan's example to work for me. It seemed to be failing on empty fields or maybe fields with trailing commas...
This one seems to handle both.
I did not write the parser code, just a wrapper around the parser function to make this work for a file. See attribution.
这是我的简单 JavaScript 代码:
Here's my simple vanilla JavaScript code:
这是另一个解决方案。这使用:
对于以下输入字符串:
输出:
这是我在可运行代码片段中对 parseCSVLine() 的实现:
Here's another solution. This uses:
For the following input string:
The code outputs:
Here's my implementation of parseCSVLine() in a runnable code snippet:
这是我的 PEG(.js) 语法,似乎在 RFC 4180 上表现良好(即它处理 处的示例http://en.wikipedia.org/wiki/Comma-separated_values):
在 http: //jsfiddle.net/knvzk/10 或 http://pegjs.majda.cz/online。在 https://gist.github.com/3362830 下载生成的解析器。
Here's my PEG(.js) grammar that seems to do ok at RFC 4180 (i.e. it handles the examples at http://en.wikipedia.org/wiki/Comma-separated_values):
Try it out at http://jsfiddle.net/knvzk/10 or http://pegjs.majda.cz/online. Download the generated parser at https://gist.github.com/3362830.
csvToArray v1.3
一个紧凑(645 字节)但兼容的函数,用于将 CSV 字符串转换为 2D 数组,符合 RFC4180 标准。
https://code.google.com/archive/p/csv- to-array/downloads
常见用法:jQuery
常见用法:JavaScript
覆盖字段分隔符
覆盖记录分隔符
覆盖跳过标题
覆盖全部
csvToArray v1.3
A compact (645 bytes), but compliant function to convert a CSV string into a 2D array, conforming to the RFC4180 standard.
https://code.google.com/archive/p/csv-to-array/downloads
Common Usage: jQuery
Common usage: JavaScript
Override field separator
Override record separator
Override Skip Header
Override all
我有一个实现作为电子表格项目的一部分。
该代码尚未经过彻底测试,但欢迎任何人使用它。
正如一些答案所指出的,如果您确实拥有 DSV 或 TSV 文件,因为它们不允许使用值中的记录和字段分隔符。另一方面,CSV 实际上可以在字段内包含逗号和换行符,这会破坏大多数正则表达式和基于拆分的方法。
I have an implementation as part of a spreadsheet project.
This code is not yet tested thoroughly, but anyone is welcome to use it.
As some of the answers noted though, your implementation can be much simpler if you actually have DSV or TSV file, as they disallow the use of the record and field separators in the values. CSV, on the other hand, can actually have commas and newlines inside a field, which breaks most regular expression and split-based approaches.
这是一个极其简单的 CSV 解析器,它处理带逗号、换行符和转义双引号的引用字段。没有分割或正则表达式。它一次扫描输入字符串 1-2 个字符并构建一个数组。
在 http://jsfiddle.net/vHKYH/ 进行测试。
Here's an extremely simple CSV parser that handles quoted fields with commas, new lines, and escaped double quotation marks. There's no splitting or regular expression. It scans the input string 1-2 characters at a time and builds an array.
Test it at http://jsfiddle.net/vHKYH/.
jQuery-CSV
这是一个 jQuery 插件,旨在作为用于将 CSV 解析为 JavaScript 数据的端到端解决方案。它处理 RFC 4180 中出现的每一个边缘情况,以及一些弹出的情况对于 Excel/Google 电子表格导出(即主要涉及空值),缺少规范。
示例:
更新:
哦,是的,我还应该提到它是完全可配置的。
更新 2:
现在它也可以与 Node.js 上的 jQuery 配合使用。因此,您可以选择使用同一库进行客户端或服务器端解析。
更新 3:
自 Google 代码关闭以来,jquery-csv 已迁移到 GitHub 。
免责声明:我也是 jQuery-CSV 的作者。
jQuery-CSV
It's a jQuery plugin designed to work as an end-to-end solution for parsing CSV into JavaScript data. It handles every single edge case presented in RFC 4180, as well as some that pop up for Excel/Google spreadsheet exports (i.e., mostly involving null values) that the specification is missing.
Example:
Update:
Oh yeah, I should also probably mention that it's completely configurable.
Update 2:
It now works with jQuery on Node.js too. So you have the option of doing either client-side or server-side parsing with the same library.
Update 3:
Since the Google Code shutdown, jquery-csv has been migrated to GitHub.
Disclaimer: I am also the author of jQuery-CSV.
您可以使用 CSVToArray() 本博客文章中提到的函数。
You can use the CSVToArray() function mentioned in this blog entry.