如何解析 CSV 数据?

新手上路,请多包涵

我在哪里可以找到一些 JavaScript 代码来解析 CSV 数据?

原文由 Pierre-Gilles Levallois 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 784
2 个回答

您可以使用此博客条目中提到的 CSVToArray() 函数。

 <script type="text/javascript">
 // ref: http://stackoverflow.com/a/1293163/2343
 // This will parse a delimited string into an array of
 // arrays. The default delimiter is the comma, but this
 // can be overriden in the second argument.
 function CSVToArray( strData, strDelimiter ){
 // Check to see if the delimiter is defined. If not,
 // then default to comma.
 strDelimiter = (strDelimiter || ",");

 // Create a regular expression to parse the CSV values.
 var objPattern = new RegExp(
 (
 // Delimiters.
 "(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +

 // Quoted fields.
 "(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +

 // Standard fields.
 "([^\"\\" + strDelimiter + "\\r\\n]*))"
 ),
 "gi"
 );

 // Create an array to hold our data. Give the array
 // a default empty first row.
 var arrData = [[]];

 // Create an array to hold our individual pattern
 // matching groups.
 var arrMatches = null;

 // Keep looping over the regular expression matches
 // until we can no longer find a match.
 while (arrMatches = objPattern.exec( strData )){

 // Get the delimiter that was found.
 var strMatchedDelimiter = arrMatches[ 1 ];

 // Check to see if the given delimiter has a length
 // (is not the start of string) and if it matches
 // field delimiter. If id does not, then we know
 // that this delimiter is a row delimiter.
 if (
 strMatchedDelimiter.length &&
 strMatchedDelimiter !== strDelimiter
 ){

 // Since we have reached a new row of data,
 // add an empty row to our data array.
 arrData.push( [] );

 }

 var strMatchedValue;

 // Now that we have our delimiter out of the way,
 // let's check to see which kind of value we
 // captured (quoted or unquoted).
 if (arrMatches[ 2 ]){

 // We found a quoted value. When we capture
 // this value, unescape any double quotes.
 strMatchedValue = arrMatches[ 2 ].replace(
 new RegExp( "\"\"", "g" ),
 "\""
 );

 } else {

 // We found a non-quoted value.
 strMatchedValue = arrMatches[ 3 ];

 }

 // Now that we have our value string, let's add
 // it to the data array.
 arrData[ arrData.length - 1 ].push( strMatchedValue );
 }

 // Return the parsed data.
 return( arrData );
 }

 </script>

原文由 Kirtan 发布,翻译遵循 CC BY-SA 3.0 许可协议

jQuery-CSV 文件

它是一个 jQuery 插件,旨在用作将 CSV 解析为 JavaScript 数据的端到端解决方案。它处理 RFC 4180 中出现的每一个边缘情况,以及一些为 Excel/Google 电子表格导出而弹出的规范缺失的情况(即,主要涉及空值)。

例子:

曲目,艺术家,专辑,年

危险,’Busta Rhymes’,’When Disaster Strikes’,1997

 // Calling this
music = $.csv.toArrays(csv)

// Outputs...
[
  ["track", "artist", "album", "year"],
  ["Dangerous", "Busta Rhymes", "When Disaster Strikes", "1997"]
]

console.log(music[1][2]) // Outputs: 'When Disaster Strikes'

更新:

哦,是的,我还应该提到它是完全可配置的。

 music = $.csv.toArrays(csv, {
  delimiter: "'", // Sets a custom value delimiter character
  separator: ';', // Sets a custom field separator character
});

更新 2:

它现在也适用于 Node.js 上的 jQuery。因此,您可以选择使用同一个库进行客户端或服务器端解析。

更新 3:

自 Google Code 关闭以来, jquery-csv 已迁移至 GitHub

免责声明:我也是 jQuery-CSV 的作者。

原文由 Evan Plaice 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题