javascript中以制表符分隔的文本文件到数组
我有一个文本文件,其中包含多个格式为的数据:javascript中以制表符分隔的文本文件到数组,javascript,arrays,Javascript,Arrays,我有一个文本文件,其中包含多个格式为的数据: 1 2016-01-01 17:23:24 0 0 1 1 2 2016-02-01 07:15:23 0 0 2 1 1 2016-03-01 12:13:24 0 0 1 1 3 2016-04-02 13:34:19 0 0 3 1 ..... 代码: 要使数组类似于: var x = [[1,2016-01-01,17:23:24][2,2016-02-01,07:15:23][1,2016-03-01,12:13:24]] 您还可以将输
1 2016-01-01 17:23:24 0 0 1 1
2 2016-02-01 07:15:23 0 0 2 1
1 2016-03-01 12:13:24 0 0 1 1
3 2016-04-02 13:34:19 0 0 3 1
.....
代码:
要使数组类似于:
var x = [[1,2016-01-01,17:23:24][2,2016-02-01,07:15:23][1,2016-03-01,12:13:24]]
您还可以将输入文件用于
因此,您可以使用文件读取器获取所选文件的内容:
function readText(filePath) {
var output = "";
if(filePath.files && filePath.files[0]) {
reader.onload = function (e) {
output = e.target.result;
displayContents(output);
};
reader.readAsText(filePath.files[0]);
var result = output.split("\t");
console.log(result);
}
}
var result = output.split("\t");
将字符串拆分为数组。按选项卡划分
您还可以在系统上的文件上创建GET请求
如果不使用jquery,请执行以下操作:
var file = "file://your path"
var toread= new XMLHttpRequest();
toread.open("GET", file, false);
toread.onreadystatechange = function ()
{
if(toread.readyState === 4)
{
if(toread.status === 200 || toread.status == 0)
{
var allText = toread.responseText;
}
}
}
toread.send(null);
var result = allText.split("\\n");
使用jquery将使它变得更简单:
$.get('file://your path', function(allText ) {
var result = (allText.split("\\n");
}, 'text')
您还可以将输入文件用于
因此,您可以使用文件读取器获取所选文件的内容:
function readText(filePath) {
var output = "";
if(filePath.files && filePath.files[0]) {
reader.onload = function (e) {
output = e.target.result;
displayContents(output);
};
reader.readAsText(filePath.files[0]);
var result = output.split("\t");
console.log(result);
}
}
var result = output.split("\t");
将字符串拆分为数组。按选项卡划分
您还可以在系统上的文件上创建GET请求
如果不使用jquery,请执行以下操作:
var file = "file://your path"
var toread= new XMLHttpRequest();
toread.open("GET", file, false);
toread.onreadystatechange = function ()
{
if(toread.readyState === 4)
{
if(toread.status === 200 || toread.status == 0)
{
var allText = toread.responseText;
}
}
}
toread.send(null);
var result = allText.split("\\n");
使用jquery将使它变得更简单:
$.get('file://your path', function(allText ) {
var result = (allText.split("\\n");
}, 'text')
你可以这样使用它
$.get('file://your path', function(allText ) {
var result = allText.split("\t");
//and access them with index value like this
alert(result.1);
});
你可以这样使用它
$.get('file://your path', function(allText ) {
var result = allText.split("\t");
//and access them with index value like this
alert(result.1);
});
谢谢你的课,但我可以很好地得到文件。我的问题是读取文件并仅将某些列放入数组中。请稍等,我会将其添加到数组中。谢谢您的课,但我可以很好地获取文件。我的问题是读取文件并只将某些列放入数组中。请稍等,我会将其添加到awnser
var x=[[12016-01-01,17:23:24][22016-02-01,07:15:23][12016-03-01,12:13:24]]
甚至不是有效的JS。请改进问题var x=[[12016-01-01,17:23:24][22016-02-01,07:15:23][12016-03-01,12:13:24]]
甚至是无效的。请改进这个问题