1 回答

TA貢獻(xiàn)1854條經(jīng)驗 獲得超8個贊
查看 split 函數(shù)將文本拆分為數(shù)組。
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')
}
})
添加回答
舉報