You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js遍历多TXT文件匹配CSV手机号时匹配结果异常问题求助

问题原因与修复方案
  • 手机号预处理错误:Windows平台下CSV文件的换行符为\r\n,仅用split('\n')拆分后,每个手机号末尾会残留\r不可见字符,无法和friendly_name里的正常手机号匹配;同时如果CSV文件末尾有空行,拆分后会生成空字符串,空字符串的includes判断永远返回真,会干扰正常匹配逻辑。
  • 循环逻辑效率过低:原有三重循环总遍历次数超过1.6亿次,额外增加了逻辑出错的概率。
修复后的代码
const fs = require('fs');
const path = require('path');

// 读取并预处理手机号列表
const csvContent = fs.readFileSync(path.join(__dirname, 'PhonesNumbersFile.csv'), 'utf8');
// 拆分后去除首尾空白、过滤空行
const phoneList = csvContent.split('\n')
  .map(phone => phone.trim())
  .filter(phone => phone);

const channelsFound = [];
const fileCount = 654;

for (let i = 0; i < fileCount; i++) {
  const filePath = path.join(__dirname, 'Channels', `channels${i}.txt`);
  const fileContent = fs.readFileSync(filePath, 'utf8');
  const fileData = JSON.parse(fileContent);

  fileData.channels.forEach(channel => {
    // 匹配任意一个手机号即符合条件
    const isMatch = phoneList.some(phone => channel.friendly_name.includes(phone));
    if (isMatch) {
      const attributes = JSON.parse(channel.attributes);
      if (attributes.long_lived) {
        channelsFound.push(channel);
      }
    }
  });
}

如果需要统计每个手机号对应的匹配结果,可以在匹配成功时同步记录关联的手机号,方便后续校验。

内容的提问来源于stack exchange,提问作者Mauro Rivero

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 00:24:03