1 回答

TA贡献1854条经验 获得超8个赞
查看 split 函数将文本拆分为数组。
const { readFileSync } = require('fs')
const file = readFileSync('./PEDEMS_01260848000112_20200908124543.txt')
const txtFile = file.toString()
const allLines = txtFile.split('\n') // Create array with lines
const invoice = allLines.slice(0,3) // Top 3 lines
const cleanLines = allLines.slice(3, allLines.length) // Everything except top 3 lines
// Loop through the lines
cleanLines.forEach(line => {
const columns = line.split(' ') // Split again on columnns
const hasTwo = columns[0].substr(0,1) // Check the first number
if (hasTwo) {
// It started with 2, you can do something
console.log('do some magic')
}
})
添加回答
举报