简体   繁体   English

使用javascript将文本文件读入数组时出现意外行为

[英]unexpected behavior when reading a text file into array using javascript

I am writing a emulator and I am trying to compare opcode logs from another working emulator.我正在编写一个模拟器,我正在尝试比较来自另一个工作模拟器的操作码日志。 I have a log of executed opcodes ready to be compared it looks something like this我有一个已执行操作码的日志可供比较,它看起来像这样

//log.txt (10000 lines long)
0
195
33
195
18...

I am reading this text file into a array using this code that looks kinda like this我正在使用这个看起来有点像这样的代码将此文本文件读入数组

//CPU.js
let log  = fs.readFileSync("./log.txt").toString().split("\n"); // read file
let logCount = 0;
function executeOP(){
let opcode = memoryRead(PC.inc()); // get opcode
  if(opcode != parseInt(log[logCount])){ // convert string to number and compare opcodes
    console.log("failed opcode " + log[logCount]); // print out failed opcode
    console.log("failed at line " + logCount); // print out failed opcode position
  }
  logCount++ // index of next opcode to compare
....
}

The problem is that parse in evaluates to NaN randomly on certain places in the test log array.问题是 parse in 在测试日志数组中的某些位置随机评估为 NaN。 It seems that extra invisible character example似乎是额外的隐形字符示例

console.log(log[2]) // 33
console.log(log[2][0]) // invisible
console.log(log[2].length) // 3? should be 2

I have tried correcting the array with (didn't work for me)我试过用(对我不起作用)更正数组

for(let i = 0; i < log.length; i++){
 log[i] = log[i].trim();
}
// also
for(let i = 0; i < log.length; i++){
 log[i] = log[i].replace(/(\r\n|\n|\r)/gm,"");
}

similar issue thread Loading text file in Javascript adds unexpected extra hidden characters类似的问题线程在 Javascript 中加载文本文件会添加意外的额外隐藏字符

Thank you谢谢

I've tried simple solution on replicated textfile and I don't really see any issue.我在复制的文本文件上尝试了简单的解决方案,但我真的没有看到任何问题。

const fs = reqiure('fs-extra')
const text = fs.readFileSync("./data.txt").toString().split("\r\n")

console.log(text[2][1]) // 3

Potential problem is with \\n in your split function, in this code there is used \\r\\n to split these lines at glance, optionally you're able to use library dedicated for working on massive text-data, I've used that library for larger linear progression in node.js so maybe it will be useful for you, it's called skale .潜在的问题在于您的拆分函数中的\\n ,在此代码中,使用\\r\\n来一目了然地拆分这些行,您可以选择使用专用于处理大量文本数据的库,我已经使用了它node.js用于更大线性进程的库,所以它可能对你有用,它被称为skale

turn out it was a few zero width spaces very frustrating but a simple fix原来这是几个零宽度的空间非常令人沮丧,但一个简单的修复

    for(let i = 0; i < log.length; i++){
        log[i] = log[i].replace(/\u200B/g,"");
    }

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

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