美文网首页
Node按行读取文件

Node按行读取文件

作者: play_0 | 来源:发表于2017-08-31 10:35 被阅读0次
  • 使用line-reader模块
    包含使用iconv-lite模块转义字符串
const fs = require('fs');
const lineReader = require('line-reader');
const path = require('path');
const iconv = require("iconv-lite");
const co = require('co');

/**
 * @method 读取小说并解析
 * @param paths 文件地址
 * @returns {Function}
 */
readerTXT(paths) {
    return function (cb) {
        co(function* () {
            let count = 0; //章节数
            let contents = [];//记录内容
            let oldTitle = '简介';//记录当前标题
            let content = [];//记录当前内容
            //调用lineReader模块的eachLine按行读取接口,并设置读取字节码,方便字符编码转义
            lineReader.eachLine(paths, {encoding: 'binary'}, function (line, last) {
                //转义
                let oldStr = new Buffer(line, 'binary');
                //默认utf8
                let str = iconv.decode(oldStr, 'utf8');
                //判断是否是gbk
                if (str.indexOf('�') !== -1) {
                    str = iconv.decode(oldStr, 'gb2312');
                }
                //判断是否按规则提取内容
                let chapterTest = /(^\第)(.{1,9})[章](\s*)(.*)|(^\[前言序章完本])(\s*)(.*)/;
                if (chapterTest.test(str)) {
                    //保存章节
                    contents.push({
                        title: oldTitle,
                        filename: count,
                        content: content
                    });
                    oldTitle = str;
                    content = [];
                    count++;
                } else {
                    if (str && (str.indexOf("第") === -1 && str.indexOf("卷") === -1) && (str.indexOf("第") === -1 && str.indexOf("部") === -1) && str.indexOf("===") === -1 && (str.indexOf("更多") === -1 && str.indexOf("http") === -1)) {
                        content.push(str);
                    }
                }
                //判断是否到最后一行
                if (last) {
                    contents.push({
                        title: oldTitle,
                        filename: count,
                        content: content
                    });
                    cb(null, contents);
                }
            });
        }).catch(function (err) {
            cb(new Error(err.message), null);
        })
    }
}

相关文章

  • Node按行读取文件

    使用line-reader模块包含使用iconv-lite模块转义字符串

  • Python IO 流

    转载请注明出处 读文件 读取整个文件 分段读取 按行读取代码 按行读取 二进制读取 写文件 文本写出 追加文件 二...

  • node.js按行读取文件

  • go实现按行读取文件

    go实现按行读取文件(附案例) 按行读取文件并筛选打印数据func ReadLineFile(fileName s...

  • gradle文件操作

    读取文件,按行读取 写文件,如果文件不存在则创建,覆盖写

  • shell读取文件三种方法

    Shell按行读取文件的3种方法 Shell按行读取文件的方法有很多,常见的三种方法如下: 要读取的文件: 写法一...

  • 常用模块

    文件操作## 读取文件 可以使用with块缩短代码 分块读取 按行读取:如果要一次性读取所有行,只需调用readl...

  • shell按行读取文件

    在shell中按行读取文件常用的方法如下: file.txt文件中内容如下:

  • 杂-文件按行读取

    背景 实际开发过程中,需要去实现一个API接口调用审计日志的存储 & 读取汇总功能。很容易想到这类数据可以用文件存...

  • Python 按行读取文件

    import re def read_file_text():s = []f = open('yingyu.txt...

网友评论

      本文标题:Node按行读取文件

      本文链接:https://www.haomeiwen.com/subject/mgzfjxtx.html