简体   繁体   English

从 txt 文件创建变量数组

[英]Creating a variable array from a txt file

I have a txt file, then i need to run all lines starting with the number 2, then put them into a array I'm using node.js This is what a tried so far.我有一个 txt 文件,然后我需要运行以数字 2 开头的所有行,然后将它们放入一个数组中我正在使用 node.js 这是迄今为止尝试过的。 The firs 3 lines, are always static, so they don't change, I'm using readFileSync to put the file into a variable.前 3 行始终是静态的,因此它们不会更改,我使用 readFileSync 将文件放入变量中。 So I use substr and already atribute the first 3 row, where they belong, the problem is, the lines starting with the number 2, are random for each file, one has 10 another 15, then i can't use substr.所以我使用 substr 并且已经分配了它们所属的前 3 行,问题是,以数字 2 开头的行对于每个文件都是随机的,一个有 10 个另外 15 个,然后我不能使用 substr。 this is what i got so far.这是我到目前为止所得到的。

import pathFile from './readDir.js'
import { readFileSync } from 'fs'
 
 
const files = pathFile()
 
 
const read = () => {
  let a = []
  for (let i = 0; i < files.length; i++)
    a.push(readFileSync(files[i],'utf-8'))
 
  return a
 
}
 
 
const pedido = read();
 
const header = {
  identifier: pedido[0].substr(1,15),
  distributorCNPJ: pedido[0].substr(16,15).trim(),
  processingDate: pedido[0].substr(31,8),
  processingHour: pedido[0].substr(39,8),
  industryCNPJ: pedido[0].substr(47,15).trim()
 
}
const invoice = {
  customerCNPJ: pedido[0].substr(65,15),
  invoiceNumber: pedido[0].substr(80,12).trim(),
  date: pedido[0].substr(92,8),
  orderType: pedido[0].substr(100,1),
  returnType: pedido[0].substr(101,1),
  comercialPointer: pedido[0].substr(102,5),
  customerInvoiceNumber: pedido[0].substr(107,15).trim(),
  //deadLine: pedido[0].substr(122,3), Nao utiliza
  agentCode: pedido[0].substr(122,4)
}
 
const deadLine = {
  code: pedido[0].substr(129,5),
  description: pedido[0].substr(135,30).trim(),
  numberQuotes: pedido[0].substr(136,3),
  discount: pedido[0].substr(39,8),
  industryCNPJ: pedido[0].substr(47,15).trim()
 
}
console.log(deadLine)
   

Text file:文本文件:

0PEDIDO OPER.LOG01260848000112 202009080000000057507378000365 
101319726100035719873499    08092020IC00000LEG585786      3009
40014820/30/40                      00300000020030040000000000033330333303334000000000000000
219873499    78949161426320000105772000ZZ
219873499    78949161438680000107826000ZZ
219873499    78960047043950000204661000ZZ
219873499    78949161456950000207343000ZZ
219873499    78949161434310000206859000ZZ
219873499    78960048101880001205531000ZZ
219873499    78960048102250000107463000ZZ
219873499    78960047257720000107926000ZZ
219873499    78949161454590000107947000ZZ
219873499    78949161451140000107584000ZZ
219873499    78949161451210000307584000ZZ
219873499    78960047150320000406739000ZZ
219873499    78949161481080000406300000ZZ
219873499    78949161456880000207826000ZZ
219873499    78960047567210000104661000ZZ
219873499    78960047684650000206859000ZZ
219873499    78960047684720000206859000ZZ
219873499    78949161430280004808309000ZZ
219873499    78949161407060000308973000ZZ
219873499    78949161406900000208792000ZZ
219873499    78960047341320000103477000ZZ
219873499    78960047226890000206859000ZZ
219873499    78949161404230000208973000ZZ
219873499    78949161411230000407100000ZZ
219873499    78960047078460001606618000ZZ
219873499    78960047208380000906135000ZZ
219873499    78960047152300000506135000ZZ
219873499    78960047039540002006859000ZZ
219873499    78949161479720000407947000ZZ
219873499    78960047600630000204000000ZZ
219873499    78960047390830000101544000ZZ
319873499    000310000000161

Check out the split function to split text into array.查看 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')
  }
})

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

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