基本Papa Parse问题

huangapple go评论81阅读模式
英文:

Basic Papa Parse Issue

问题

我在控制台中尝试使用Papa Parse解析CSV文件时遇到了以下的UndetectableDelimiter错误:

基本Papa Parse问题

数据数组中没有包含任何内容。Test2.csv是一个由Excel创建并保存的逗号分隔文件,包含4列和4行。该文件位于与HTML文件相同的文件夹中。在此查看CSV文件:CSV文件

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.1.0/papaparse.min.js"></script>

<script>
    $(function() { 
        
        var results = Papa.parse('test2.csv', {
            delimiter: "",
            newline: "",
            complete: function(results) {
                console.log(results);
                data = results.data;
            }
        });
    });
</script>
英文:

I'm getting the following UndetectableDelimiter error in the console when trying to parse CSV files with Papa Parse:

基本Papa Parse问题

The data array does not contain anything. Test2.csv is a comma delimited file created and saved in Excel. It has 4 columns and 4 rows. The file is located in the same folder as the HTML file. See the CSV file here: https://filebin.net/f9kgeercbsw775lv

The CSV file

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

    &lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.1.0/papaparse.min.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;
	$(function() { 
		
	var results = Papa.parse(&#39;test2.csv&#39;, {
  delimiter: &quot;&quot;,  
    newline: &quot;&quot;, 
  complete: function(results) {
    console.log(results);
    data = results.data;
  }
});

<!-- end snippet -->

答案1

得分: 0

你正在将空字符串指定为分隔符。将你的分隔符设置为逗号,至少可以避免该错误。

delimiter: ','

**编辑:**我使用Papa.parse()的唯一方式是通过文件上传。以下是示例:

<label for="theCSV">CSV文件:</label>
<input type="file" id="theCSV" name="theCSV" />

<div id="results"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.1.0/papaparse.min.js"></script>

<script>
  $(function() {
    var csv;

    $("#theCSV").change(function(e) {
      Papa.parse(e.target.files[0], {
        download: true,
        header: true,
        delimiter: ',',
        skipEmptyLines: true,
        error: function(err, file, inputElem, reason) {
          $('#results').append('错误:' + err + ' : ' + reason + '<br>');
          return false;
        },
        complete: function(results) {
          $('#results').append(JSON.stringify(results.data) + '<br>');
        }
      });
    });
  });
</script>

Note: The code you provided contains HTML and JavaScript, and I've translated the relevant parts as requested.

英文:

You're specifying an empty string as the delimiter. Set your delimiter to comma and you'll at least avoid that error.

delimiter: &#39;,&#39;,

Edit: The only way I've used Papa.parse() is with a file upload. Here's how:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;label for=&quot;theCSV&quot;&gt;CSV file:&lt;/label&gt;
&lt;input type=&quot;file&quot; id=&quot;theCSV&quot; name=&quot;theCSV&quot; /&gt;


&lt;div id=&quot;results&quot;&gt;&lt;/div&gt;

&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdnjs.cloudflare.com/ajax/libs/PapaParse/5.1.0/papaparse.min.js&quot;&gt;&lt;/script&gt;

&lt;script&gt;
  $(function() {
    var csv;

    $(&quot;#theCSV&quot;).change(function(e) {
      Papa.parse(e.target.files[0], {
        download: true,
        header: true,
        delimiter: &#39;,&#39;,
        skipEmptyLines: true,
        error: function(err, file, inputElem, reason) {
          $(&#39;#results&#39;).append(&#39;Error: &#39; + err + &#39; : &#39; + reason + &#39;&lt;br&gt;&#39;);
          return false;
        },
        complete: function(results) {
          $(&#39;#results&#39;).append(JSON.stringify(results.data) + &#39;&lt;br&gt;&#39;);
        }
      });
    });
  });
&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月4日 01:45:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/59583049.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定