简体   繁体   English

如何将 txt 文件中的数据转换为 Node 中的对象数组

[英]How to turn data from a txt file into an array of objects in Node

I'm new to Node, and I have a text file that has data like this我是 Node 的新手,我有一个文本文件,其中包含这样的数据

Date    Open    High    Low Close   Volume
11-Jun-19   163.3   164.54  162.74  163.1   158470476
10-Jun-19   165.31  165.4   164.37  164.8   105667060
7-Jun-19    163.85  164.95  163.14  164.8   188337725 
... 

I would like to create an array of objects like this我想创建一个这样的对象数组

[{
    Date: "11-Jun-19",
    Open: 163.22,
    High: 164.28,
    Low: 163.05,
    Close: 163.88,
    Volume: 5854647
}, {
    Date: "12-Jun-19",
    Open: 163.22,
    High: 164.28,
    Low: 163.05,
    Close: 163.88,
    Volume: 5854647
}, {
    Date: "15-Jun-19",
    Open: 163.22,
    High: 164.28,
    Low: 163.05,
    Close: 163.88,
    Volume: 5854647
}] 

How can I do this?我怎样才能做到这一点? This was my attempt:这是我的尝试:

const lineReader = require('line-reader');

lineReader.eachLine('input.txt', function (line) {
  let results = [];
  let divide = line.split("   ");
  for (let i = 0; i < divide.length; i++) {
    let field = divide[i].split("/t");
    results.push({
      date: field[0],
      open: field[1],
      high: field[2],
      low: field[3],
      close: field[4],
      volume: field[5]
    });
  }
  console.log(results);
}); 

But this create an array for each object and I get all the data showing under date like this:但这会为每个对象创建一个数组,我会得到所有显示在date下的数据,如下所示:

[
  {
    date: '11-Jun-19\t163.3\t164.54\t162.74\t163.1\t158470476',
    open: undefined,
    high: undefined,
    low: undefined,
    close: undefined,
    volume: undefined
  }
]
[
  {
    date: '10-Jun-19\t165.31\t165.4\t164.37\t164.8\t105667060',
    open: undefined,
    high: undefined,
    low: undefined,
    close: undefined,
    volume: undefined
  }
]
... 

You can try readline internal module, by the way ( see this example ), if your file is big and you do need line by line processing:顺便说一句,如果您的文件很大并且确实需要逐行处理,您可以尝试readline内部模块( 请参阅此示例):

const fs = require('fs');
const readline = require('readline');

async function processLineByLine() {
  const fileStream = fs.createReadStream('test.txt');

  const rl = readline.createInterface({
    input: fileStream,
    crlfDelay: Infinity,
  });

  let headers = null;
  const data = [];

  for await (const line of rl) {
    const row = line.split(/\s+/);

    if (headers === null) { // So this is the first line.
      headers = row;
    } else {
      const entry = {};
      for (let i = 0; i < row.length; i++) {
        const header = headers[i];
        const cell = row[i];

        entry[header] = header === 'Date' ? cell : Number(cell);
        // Or more generally:
        // const mayBeNumber = Number(cell);
        // entry[header] = Number.isNaN(mayBeNumber) ? cell : mayBeNumber;
      }
      data.push(entry);
    }
  }

  console.log(data);
}

processLineByLine();

This code should work:此代码应该工作:

        const fs = require('fs');
    const file = fs.readFileSync('data.txt');
    //Read Each line separately and add them to an array. and remove first line of your file;
    const array = file.split('\n').slice(1);

    let objects = [], i, data;
    for(let row of array){
        i = 0;
        //separate each line data and add them to data array
        data = row.split(/ +/);
        objects.push({
            Date: data[i],
            Open: data[++i],
            High: data[++i],
            Low: data[++i],
            Close: data[++i],
            Volume:data[++i]
        })
    }

Maybe use existing solutions instead of reinventing the wheel?也许使用现有的解决方案而不是重新发明轮子?

For example Just change separator to space from ',', probably '\\s'例如,只需将分隔符从 ',' 更改为空格,可能是 '\\s'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM