【问题标题】:Node file parsing by empty line not working空行解析节点文件不起作用
【发布时间】:2020-12-23 15:08:35
【问题描述】:

我在 txt 文件中有一些数据,格式如下:

byr:1985
eyr:2021 iyr:2011 hgt:175cm pid:163069444 hcl:#18171d

eyr:2023
hcl:#cfa07d ecl:blu hgt:169cm pid:494407412 byr:1936

ecl:zzz
eyr:2036 hgt:109 hcl:#623a2f iyr:1997 byr:2029
cid:169 pid:170290956

hcl:#18171d ecl:oth
pid:266824158 hgt:168cm byr:1992 eyr:2021

我已经有了逐行解析txt文件的函数:

function parse_file_by_line(folder, file_name) {
  // Read the input file line by line, creating an array of inputs.
  const input_file = path.join(__dirname, folder, file_name);
  return (input_array = fs.readFileSync(input_file).toString().split("\r\n"));
}

但是,我想在空行上进行解析。 (文本文件中的空格)。有人建议在“\n\n”上拆分,我尝试过,但最终将所有数据放入一个大数组元素中。”我想将它拆分为空行,并将数据拆分为一个数组元素。例如,第一个索引将是“byr:1985 eyr:2021 iyr:2011 hgt:175cm pid:163069444 hcl:#18171d”。

【问题讨论】:

  • 使用拆分(“\r\n\r\n”)。 Windows 在每一行结尾都有 \r\n 而 linux 只使用 \n。
  • @John 你我的朋友是救生员,谢谢!

标签: javascript node.js file parsing


【解决方案1】:

我想提供一种检查文件字符串的方法。你可以使用JSON.stringify 来检查你的文件有什么样的字符串。通过这种方式,你会发现你的行尾是什么。

const fs = require('fs')
const data = fs.readFileSync("./file.txt")
console.log(JSON.stringify(data.toString()));

在我的 Mac 中,这是输出。

"byr:1985\neyr:2021 iyr:2011 hgt:175cm pid:163069444 hcl:#18171d\n\neyr:2023\nhcl:#cfa07d ecl:blu hgt:169cm pid:494407412 byr:1936\n\necl:zzz\neyr:2036 hgt:109 hcl:#623a2f iyr:1997 byr:2029\ncid:169 pid:170290956\n\nhcl:#18171d ecl:oth\npid:266824158 hgt:168cm byr:1992 eyr:2021"

【讨论】:

    【解决方案2】:

    使用拆分(“\r\n\r\n”)。 Windows 在每一行结尾都有 \r\n,而 linux 只使用 \n。

    【讨论】:

      【解决方案3】:

      您可以使用stream 方法,这意味着不会将所有文件读入内存。这在处理大文件时很有用。以下也处理 CRLF:

      const fs = require('fs');
      const readline = require('readline');
      
      async function processLineByLine() {
        const fileStream = fs.createReadStream('input.txt');
      
        const rl = readline.createInterface({
          input: fileStream,
          crlfDelay: Infinity
        });
        // Note: we use the crlfDelay option to recognize all instances of CR LF
        // ('\r\n') in input.txt as a single line break.
      
        for await (const line of rl) {
          // Each line in input.txt will be successively available here as `line`.
          console.log(`Line from file: ${line}`);
        }
      }
      
      processLineByLine();
      

      欲了解更多信息,请参阅this

      现在,您可以自定义 for 循环来检测空行,如下所示:

       var lines = []
      
       for await (const line of rl) {
          // Each line in input.txt will be successively available here as `line`.
          
          if (line === ''){
             if (lines.length > 0)
               process_chunk(lines);
             lines = [] //empty the array and make it ready for next iteration
      }
          else
            lines.push(line)
        }
      
      function process_chunk(lines) {
       //now, you have an array of consequtive non empty lines here
      }
      

      【讨论】:

        猜你喜欢
        • 2013-03-09
        • 1970-01-01
        • 1970-01-01
        • 2014-03-06
        • 1970-01-01
        • 2016-09-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多