TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

CSVjs: Basic CSV parsing and encoding in JavaScript

66 pointsby knrzalmost 11 years ago

11 comments

userbinatoralmost 11 years ago
&gt; text.split(&quot;\n&quot;)<p>I don&#x27;t want to be negative but this will not work on CSV files that contain newlines inside columns. You should also implement escaping quotes - this is <i>very important</i> to ensure that the data can round-trip! Good for a first attempt, but as simple as CSV may look, there are subtle points that you should be aware of if you want to handle most if not all CSV out there. I&#x27;ve worked with lots of other implementations that get these little things wrong too, and it is particularly aggravating when CSV is supposed to be a pretty standard interchange format for tabular data. Please refer to RFC4180 for the details.
评论 #7794957 未加载
评论 #7795442 未加载
评论 #7795262 未加载
hglaseralmost 11 years ago
This kind of thing is incredibly useful.<p>Large frameworks are great, but generally I only adopt them when starting a new project; or the occasional large refactoring project.<p>Whereas targeted problem-solving libraries like this one get adopted all day long. Next time I need to parse a CSV on the client, you can bet the first thing I&#x27;ll do is Google to see if there&#x27;s a library I can download and be done with it.<p>So, great job solving a targeted problem! Keep building things and keep contributing. People will find code like this endlessly useful.
评论 #7795106 未加载
knrzalmost 11 years ago
First anything I&#x27;ve contributed to the community.<p>As a 16 year-old just getting into software development, it&#x27;d be amazing to get feedback from hn.
评论 #7794921 未加载
评论 #7795096 未加载
评论 #7806949 未加载
评论 #7794907 未加载
评论 #7794926 未加载
nthitzalmost 11 years ago
d3.js provides a pretty solid JS csv parser <a href="https://github.com/mbostock/d3/blob/master/src/dsv/dsv.js" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mbostock&#x2F;d3&#x2F;blob&#x2F;master&#x2F;src&#x2F;dsv&#x2F;dsv.js</a><p><a href="https://github.com/mbostock/d3/wiki/CSV" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;mbostock&#x2F;d3&#x2F;wiki&#x2F;CSV</a>
评论 #7794995 未加载
seldoalmost 11 years ago
If you are doing large-scale CSV parsing or encoding in Node, you may also find these two packages useful:<p><a href="https://www.npmjs.org/package/binary-csv" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.org&#x2F;package&#x2F;binary-csv</a><p><a href="https://www.npmjs.org/package/csv-write-stream" rel="nofollow">https:&#x2F;&#x2F;www.npmjs.org&#x2F;package&#x2F;csv-write-stream</a><p>They are both written with handling very large files in mind, so they use buffers and streams for i&#x2F;o.
Monkeygetalmost 11 years ago
The CSV &#x27;format&#x27; is hell. I just wrote a blog highlighting some of the issues : <a href="http://tburette.github.io/blog/2014/05/25/so-you-want-to-write-your-own-CSV-code/" rel="nofollow">http:&#x2F;&#x2F;tburette.github.io&#x2F;blog&#x2F;2014&#x2F;05&#x2F;25&#x2F;so-you-want-to-wri...</a>
评论 #7806833 未加载
EvanPlaicealmost 11 years ago
Ever heard of jquery-csv?<p>I wrote the jquery-csv over two years ago with the goal of being the first completely RFC compliant CSV parser for Javascript.<p>It integrates with the CSV namespace but doesn&#x27;t depend on it, uses pure vanilla JS, works with Node.js.<p>At the very least, if you want to claim RFC compliance you should have a test runner that verifies that your code doesn&#x27;t break on the edge cases.<p><a href="https://code.google.com/p/jquery-csv/source/browse/test/test.html" rel="nofollow">https:&#x2F;&#x2F;code.google.com&#x2F;p&#x2F;jquery-csv&#x2F;source&#x2F;browse&#x2F;test&#x2F;test...</a><p>Once you have your state machine working, your best bet to optimize speed is by limiting string copy operations. I managed this by using a regex tokenizer that groups any non-terminals (ex data between quotes).<p>I wrote it with the intent of providing a lib that can effectively parse CSV data from the browser (loaded remotely via AJAX or locally via the HTML5 File API).<p>The biggest weakness of parsing CSV on the browser is the inability to process data streams. That 2GB memory limit on JS scrips in the browser becomes a fundamental weakness when you&#x27;re trying to process large CSV files.<p>CSV in general is terrible for data storage, unless it&#x27;s only used for serialization because arbitrarily reading any point in the input data stream requires the parser to start from the beginning. You&#x27;re basically screwed if you can&#x27;t hold the whole dataset in memory as a 2D array.
maxericksonalmost 11 years ago
Consider starting at the default behavior with the documentation. For example, in the parse section, if no header option is provided, the first row will be returned as an array correct?<p>Start there: &quot;The default behavior is to return each row of the CSV as an array.&quot; then &quot;If the CSV file has a header, pass in {header: true} to get back an object using the header values as keys.&quot; then &quot;If the file does not have a header, pass in ...&quot;.<p>Also, can a user override a header that is present in the data?
评论 #7795366 未加载
mrfusionalmost 11 years ago
So will this let me make a csv file in JavaScript and actually then let the user download it as a file?<p>That would really simplify a lot of my workflows instead of having to make separate csv and HTML views I django.
评论 #7796353 未加载
评论 #7795150 未加载
soggypopsiclealmost 11 years ago
in case someone wants a more mature parser: <a href="https://github.com/koles/ya-csv" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;koles&#x2F;ya-csv</a>
IvanK_netalmost 11 years ago
Hey, I needed JS CSV parser few days ago. Here is my parser, I bet it is 100 times faster than yours :)<p>function parseCSV(str) { var obj = {}; var lines = str.split(&quot;\n&quot;); var attrs = lines[0].split(&quot;,&quot;); for(var i=0; i&lt;attrs.length; i++) obj[attrs[i]] = []; for(var i=1; i&lt;lines.length-1; i++) { var line = lines[i].split(&quot;,&quot;); for(var j=0; j&lt;line.length; j++) obj[attrs[j]].push(line[j]); } return obj; }
评论 #7794958 未加载
评论 #7795132 未加载