【问题标题】:How can I read a text file as an array如何将文本文件作为数组读取
【发布时间】:2020-01-25 21:27:42
【问题描述】:

我有一个如下所示的文本文件(不是 json):

['a', 'b', 'c', 'd']
['e', 'f', 'g', 'h']

我如何读取它并放入 2 个数组:['a', 'b', 'c', 'd'] 和 ['e', 'f', 'g', 'h']?

我正在使用它来读取文件:

jQuery.get('filename.txt', function(data){
 alert(data);
});

【问题讨论】:

  • 文本文件中的值是否看起来像[a, b, c, d]['a', 'b', 'c', 'd']
  • @ajaiJothi 哦,我的帖子搞错了。看起来像['a', 'b', 'c', 'd']

标签: javascript arrays text


【解决方案1】:

解决方案 1:

  1. 多行分割字符串(\r\n)
  2. 循环遍历拆分的字符串数组并将单引号替换为双引号,使其成为有效的 JSON 字符串
  3. JSON.parse解析JSON字符串

const exampleData = `['a', 'b', 'c', 'd']
['e', 'f', 'g', 'h']`;

const multiLineTextToArray = (txt) => {
  return (txt.match(/[^\r\n]+/g) || []).map((line) => {
    // replace single quote with double quote to make it proper json string
    // then parse the string to json
    return JSON.parse(line.replace(/\'/g, '\"'));
  });  
};

/**
jQuery.get('filename.txt', function(data){
 alert(multiLineTextToArray(data));
});
*/

// example
console.log(multiLineTextToArray(exampleData));

解决方案2:构造一个有效的JSON数组

  1. 将多行(\r\n) 替换为','
  2. 用双引号替换单引号
  3. []包裹整个字符串
  4. JSON.parse解析JSON字符串

const exampleData = `['a', 'b', 'c', 'd']
['e', 'f', 'g', 'h']`;

const multiLineTextToArray = (txt) => {
  return JSON.parse(`[${txt.replace(/[\r\n]+/g, ',').replace(/\'/gm, '\"')}]`);
};

/**
jQuery.get('filename.txt', function(data){
 alert(multiLineTextToArray(data));
});
*/

// example
console.log(multiLineTextToArray(exampleData));

【讨论】:

    【解决方案2】:

    我的处理方法如下:

    data 包含 .txt 文件中的所有信息。所以我会尝试使用这样的拆分函数逐行读取文件。

    var arrays = []; 
    
    jQuery.get('filename.txt', function(data){
    
    var splitted = data.split("\n"); // --> should return an array for each line 
    
    // Loop through all lines 
    for(var i = 0; i < splitted.length; i++) 
    {
    
    var line = splitted[i]; 
    
    // Now you could remove [ and ] from string  
    var removed = line.replace('[','').replace(']','');
    
    // Now you can split all values by using , delimiter
    var values = removed.split(',');  
     var array = []; // this will be a single array
    
    // Now you can iterate through all values and add them to your array
    for(var c = 0; c < values.length; c++) 
    {
    
    var value = values[c]; 
    
    // Now you can add this value to your array
    // this depends on the data structure you are using in JS 
    // for example
    array.push(value);  
    
    }
    
    // NOw all values are stored in first array so add it to the multi array
    // this is an array in an array
    arrays.push(array); 
    
    }
    
    });
    

    【讨论】:

    • 我喜欢这个,但如何将特定值添加到特定数组?
    • 每一行包含每个数组的数据,您可以使用索引为 i 的多维数组来存储所有数据。
    • 啊,所以我可以做类似splitted[i].push(value)?
    • 你需要为此定义一个自己的变量——我会在代码中添加它,但基本上你是对的
    • 等等,我还是一头雾水。我需要 2 个完全独立的数组。如果我这样做,我不会只有一个数组,其中所有值都在另一个数组中吗?
    猜你喜欢
    • 2015-02-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2011-11-27
    • 1970-01-01
    • 2019-08-31
    相关资源
    最近更新 更多