AlfieriChou / alfierichou.github.io

AlfieriChou的博客
https://alfierichou.com
4 stars 0 forks source link

按行读取CSV文件 #15

Open AlfieriChou opened 2 years ago

AlfieriChou commented 2 years ago
const fs = require('fs')
const readline = require('readline')
const Stream = require('stream')
const got = require('got')

const { ACCESS_TOKEN } = process.env
const url = 'https://t.cn'
const accessToken = ACCESS_TOKEN

const start = async () => {
  const ret = await got(url, {
    headers: {
      'Access-Token': accessToken
    }
  })

  const inStream = new Stream.Readable()
  // const inStream = new fs.createReadStream('test.csv')
  const outStream = new Stream()

  inStream.push(ret.body)
  inStream.push(null)

  const rl = readline.createInterface(inStream, outStream)

  let lineCount = 0
  const data = []

  rl.on('line', line => {
    lineCount += 1
    const arr = line.split(',')
    const [
      name, age
    ] = arr
    data.push({
      name, age
    })
  })

  rl.on('close', () => {
    console.log('----', lineCount, data)
  })

}

start()
AlfieriChou commented 6 months ago
const readline = require('readline');
const fs = require('fs');

// 指定要读取的文件路径、起始行和结束行
const filePath = 'file.txt';
const startLine = 3; // 起始行
const endLine = 7;   // 结束行

// 创建逐行读取接口
const rl = readline.createInterface({
  input: fs.createReadStream(filePath) // 指定要读取的文件路径
});

let currentLine = 1;

// 逐行读取文件内容
rl.on('line', (line) => {
  if (currentLine >= startLine && currentLine <= endLine) {
    console.log(`Line ${currentLine}: ${line}`);
  }

  // 如果当前行数已经达到结束行,关闭读取接口
  if (currentLine === endLine) {
    rl.close();
  }

  currentLine++;
});

// 当文件读取完成时触发
rl.on('close', () => {
  console.log('File reading completed.');
});
AlfieriChou commented 6 months ago

官方方案

https://nodejs.cn/api-v16/readline/example_read_file_stream_line_by_line.html