英文:
Basic Papa Parse Issue
问题
我在控制台中尝试使用Papa Parse解析CSV文件时遇到了以下的UndetectableDelimiter错误:
数据数组中没有包含任何内容。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:
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
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
<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;
}
});
<!-- 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: ',',
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 -->
<label for="theCSV">CSV file:</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('Error: ' + err + ' : ' + reason + '<br>');
return false;
},
complete: function(results) {
$('#results').append(JSON.stringify(results.data) + '<br>');
}
});
});
});
</script>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论