服务器端CSV文件,将其转换为JavaScript数组
我正在做一个项目,我需要能够更新一个HTML表,我会通过javascript来完成。我还没有为这个项目创建一个HTML站点,因为我将尝试将CSV文件转换为Java数组,这将更新HTML表 我一直在尝试与爸爸解析,但它不会为我工作。我没有npm的经验,也没有像Papa parsa那样安装和使用东西的经验。 我已经找到了一个网站,它有一个伟大的功能,将CSV转换为一个数组。这个函数的一个问题是,我不能在服务器上获取一个本地文件,并像处理字符串一样将其放入函数中。也许我忽略了什么服务器端CSV文件,将其转换为JavaScript数组,javascript,html,arrays,csv,papaparse,Javascript,Html,Arrays,Csv,Papaparse,我正在做一个项目,我需要能够更新一个HTML表,我会通过javascript来完成。我还没有为这个项目创建一个HTML站点,因为我将尝试将CSV文件转换为Java数组,这将更新HTML表 我一直在尝试与爸爸解析,但它不会为我工作。我没有npm的经验,也没有像Papa parsa那样安装和使用东西的经验。 我已经找到了一个网站,它有一个伟大的功能,将CSV转换为一个数组。这个函数的一个问题是,我不能在服务器上获取一个本地文件,并像处理字符串一样将其放入函数中。也许我忽略了什么 我让Papa Pa
- 我让Papa Parse以某种方式工作,但我不知道如何在本地文件上使用它,所以我有两个代码来完成这项工作,但我不知道如何让它们完成,也不知道结果有多好
- 我刚得到一个随机的CSV文件作为测试文件。Normal.csv来自papa parse网站
<head>
<title>Test af Papa Parse</title>
</head>
<body>
<p>Hey - Test paraghaph</p>
<script src="node_modules/papaparse/papaparse.min.js"></script>
<script src="node_modules/jquery/dist/jquery.min.js"></script>
/*edit: src="http://localhost/test/node_modules/papaparse/papaparse.min.js"*/
/*edit: src="http://localhost/test/node_modules/jquery/dist/jquery.min.js"*/
<script>
var config = {
download: true,
// rest of config ...
delimiter: "", // auto-detect
newline: "", // auto-detect
quoteChar: '"',
escapeChar: '"',
header: false,
trimHeaders: false,
dynamicTyping: false,
preview: 0,
encoding: "",
worker: false,
comments: false,
step: undefined,
complete: undefined,
error: undefined,
download: false,
skipEmptyLines: false,
chunk: undefined,
fastMode: undefined,
beforeFirstChunk: undefined,
withCredentials: undefined,
transform: undefined
}
var data = csv2array("http://localhost/test/normal.csv")
var data2 = Papa.parse("http://localhost/test/normal.csv", config)
console.log("papa parsa - direktly: "+ Papa.parse("http://localhost/test/normal.csv", config))
console.log(data)
console.log("data2 = "+data2)
console.log(data2);
/**
* Convert data in CSV (comma separated value) format to a javascript array.
*
* Values are separated by a comma, or by a custom one character delimeter.
* Rows are separated by a new-line character.
*
* Leading and trailing spaces and tabs are ignored.
* Values may optionally be enclosed by double quotes.
* Values containing a special character (comma's, double-quotes, or new-lines)
* must be enclosed by double-quotes.
* Embedded double-quotes must be represented by a pair of consecutive
* double-quotes.
*
* Example usage:
* var csv = '"x", "y", "z"\n12.3, 2.3, 8.7\n4.5, 1.2, -5.6\n';
* var array = csv2array(csv);
*
* Author: Jos de Jong, 2010
*
* @param {string} data The data in CSV format.
* @param {string} delimeter [optional] a custom delimeter. Comma ',' by default
* The Delimeter must be a single character.
* @return {Array} array A two dimensional array containing the data
* @throw {String} error The method throws an error when there is an
* error in the provided data.
*/
function csv2array(data, delimeter) {
// Retrieve the delimeter
if (delimeter == undefined)
delimeter = ',';
if (delimeter && delimeter.length > 1)
delimeter = ',';
// initialize variables
var newline = '\n';
var eof = '';
var i = 0;
var c = data.charAt(i);
var row = 0;
var col = 0;
var array = new Array();
while (c != eof) {
// skip whitespaces
while (c == ' ' || c == '\t' || c == '\r') {
c = data.charAt(++i); // read next char
}
// get value
var value = "";
if (c == '\"') {
// value enclosed by double-quotes
c = data.charAt(++i);
do {
if (c != '\"') {
// read a regular character and go to the next character
value += c;
c = data.charAt(++i);
}
if (c == '\"') {
// check for escaped double-quote
var cnext = data.charAt(i+1);
if (cnext == '\"') {
// this is an escaped double-quote.
// Add a double-quote to the value, and move two characters ahead.
value += '\"';
i += 2;
c = data.charAt(i);
}
}
}
while (c != eof && c != '\"');
if (c == eof) {
throw "Unexpected end of data, double-quote expected";
}
c = data.charAt(++i);
}
else {
// value without quotes
while (c != eof && c != delimeter && c!= newline && c != ' ' && c != '\t' && c != '\r') {
value += c;
c = data.charAt(++i);
}
}
// add the value to the array
if (array.length <= row)
array.push(new Array());
array[row].push(value);
// skip whitespaces
while (c == ' ' || c == '\t' || c == '\r') {
c = data.charAt(++i);
}
// go to the next row or column
if (c == delimeter) {
// to the next column
col++;
}
else if (c == newline) {
// to the next row
col = 0;
row++;
}
else if (c != eof) {
// unexpected character
throw "Delimiter expected after character " + i;
}
// go to the next character
c = data.charAt(++i);
}
return array;
}
</script>
</body>
测试af Papa解析
嘿,帕拉哈
/*编辑:src=”http://localhost/test/node_modules/papaparse/papaparse.min.js"*/
/*编辑:src=”http://localhost/test/node_modules/jquery/dist/jquery.min.js"*/
变量配置={
下载:对,
//配置的其余部分。。。
分隔符:“”,//自动检测
换行:,//自动检测
quoteChar:“”,
escapeChar:“”,
标题:false,
错误:错误,
动态打字:错误,
预览:0,
编码:“,
工人:错,
评论:错,
步骤:未定义,
完成:未定义,
错误:未定义,
下载:错,
skipEmptyLines:错误,
块:未定义,
fastMode:未定义,
beforeFirstChunk:未定义,
withCredentials:未定义,
变换:未定义
}
var数据=CSV2阵列(“http://localhost/test/normal.csv")
var data2=Papa.parse(“http://localhost/test/normal.csv“,配置)
log(“papa parsa-direkly:+papa.parse(”http://localhost/test/normal.csv“,配置)
console.log(数据)
console.log(“data2=“+data2”)
console.log(data2);
/**
*将CSV(逗号分隔值)格式的数据转换为javascript数组。
*
*值由逗号分隔,或由自定义的单字符分隔符分隔。
*行由新行字符分隔。
*
*将忽略前导空格和尾随空格以及制表符。
*值可以选择用双引号括起来。
*包含特殊字符(逗号、双引号或新行)的值
*必须用双引号括起来。
*嵌入的双引号必须由一对连续的
*双引号。
*
*用法示例:
*var csv='“x”,“y”,“z”\n12.3,2.3,8.7\n4.5,1.2,-5.6\n';
*var阵列=CSV2阵列(csv);
*
*作者:何塞·德容,2010年
*
*@param{string}data以CSV格式显示数据。
*@param{string}delimeter[可选]自定义delimeter。默认情况下,逗号为“”
*Delimeter必须是单个字符。
*@return{Array}Array包含数据的二维数组
*@throw{String}error当出现错误时,该方法会抛出错误
*提供的数据中存在错误。
*/
功能csv2array(数据、测力计){
//取回delimeter
如果(delimeter==未定义)
delimeter=',';
如果(delimeter和delimeter.length>1)
delimeter=',';
//初始化变量
变量换行符='\n';
var eof=“”;
var i=0;
var c=数据特征(i);
var行=0;
var-col=0;
var数组=新数组();
而(c!=eof){
//跳过空白
而(c=''| | c='\t'| | c='\r'){
c=data.charAt(++i);//读取下一个字符
}
//获得价值
var值=”;
如果(c=='\'){
//用双引号括起来的值
c=数据字符(++i);
做{
如果(c!=“\”){
//读取一个常规字符并转到下一个字符
值+=c;
c=数据字符(++i);
}
如果(c=='\'){
//检查转义的双引号
var cnext=data.charAt(i+1);
如果(cnext=='\''){
//这是一个转义的双引号。
//在值中添加双引号,并向前移动两个字符。
值+=“\”;
i+=2;
c=数据字符(i);
}
}
}
而(c!=eof&&c!='\'');
如果(c==eof){
抛出“数据意外结束,需要双引号”;
}
c=数据字符(++i);
}
否则{
//不带引号的值
而(c!=eof&&c!=delimeter&&c!=newline&&c!=''&&c!='\t'&&c!='\r'){
值+=c;
c=数据字符(++i);
}
}
//将值添加到数组中
如果(array.length我认为你最好的选择是简化一切,直到你完全弄清楚你需要什么。这是一个非常基本的Papa.parse。你不需要配置文件,除非你正在做一些特别需要它的事情。下面是一个
<html>
<head>
<script data-require="jquery@3.1.1" data-semver="3.1.1" src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.6.1/papaparse.min.js"></script>
</head>
<body>
<script>
let csvString = '2018-06-29,2018-06-29,111211,15:35:00,77,15:50:00,,Blah,Internet User,,Baln bla,0,4,0,0,0,$516.00 ,$120.00 ,$396.00 ,$19.80 ,$415.80 ,,$0.00 ,$0.00 ,$415.80';
//let array = Papa.parse(csvString);
//console.log(array);
let array = Papa.parse('http://localhost/test/filename.csv',{download:true});
console.log(array);
</script>
</body>
</html>
<head>
<meta charset="UTF-8">
</head>
<body>
<!--Loading Papa.parse-->
<script src="http://localhost/test/node_modules/papaparse/papaparse.min.js"></script>
<!--The script-->
<script>
//The file with the csv data
var CSVFile = "http://localhost/test/download.csv"
//papa.parse function, which converts CSV file to array
function parse() {
Papa.parse(CSVFile,{
download: true, //When linking an URL the download must be true
header: true, //makes the header in front of every data in the array
complete: function (results) { //Runs log function, with results from the conversion
log(results);
}
});
}
//Papa.parse does it own callback and launches this function when done
function log(arrayFromPapa) {
//Writes the array in the console
console.log(arrayFromPapa);
//Makes the array a global array
array = arrayFromPapa
}
//luanching the program
parse()
</script>
</body>