当前位置: 首页 > news >正文

彭干泉 网站开发动易网站建设工作室

彭干泉 网站开发,动易网站建设工作室,门户网站建设定制,wordpress 仿美文说明:我希望用nodejs 写一个小工具,去除本地txt文件中的html字符,去除字符后生成新的文件,同样保存在桌面 文件的具体位置是C:\Users\Administrator\Desktop\file.txt step1:C:\Users\Administrator\WebstormProjects\untitled4\f…

说明:我希望用nodejs 写一个小工具,去除本地txt文件中的html字符,去除字符后生成新的文件,同样保存在桌面 文件的具体位置是C:\Users\Administrator\Desktop\file.txt
step1:C:\Users\Administrator\WebstormProjects\untitled4\file.js

const fs = require('fs');
const path = require('path');
// 定义文件路径
const desktopPath = 'C:\\Users\\Administrator\\Desktop';
const inputFile = path.join(desktopPath, 'file.txt');
const outputFile = path.join(desktopPath, 'clean_file.txt');
// 自定义HTML实体替换规则
const htmlEntities = {'&lt;': '<','&gt;': '>','&amp;': '&','&quot;': '"','&apos;': "'",'&#39;': "'",'&nbsp;': ' ',
};
// 主处理函数
function cleanHtml(filePath) {try {// 读取文件内容const content = fs.readFileSync(filePath, 'utf8');// 分步骤清理内容let cleaned = content// 移除HTML标签.replace(/<[^>]+>/g, '')// 替换HTML实体.replace(/&(?:[a-z]+|#\d+);/gi, (match) =>htmlEntities[match.toLowerCase()] || match)// 清理多余换行和空格.replace(/\n{3,}/g, '\n\n').replace(/ {2,}/g, ' ');// 写入新文件fs.writeFileSync(outputFile, cleaned, 'utf8');console.log(`文件清理完成,已保存至:${outputFile}`);console.log(`清理前字符数:${content.length}`);console.log(`清理后字符数:${cleaned.length}`);console.log(`移除字符数:${content.length - cleaned.length}`);} catch (error) {console.error('处理文件时发生错误:');console.error(error.message);process.exit(1);}
}
// 执行清理
if (require.main === module) {// 检查源文件是否存在if (!fs.existsSync(inputFile)) {console.error(`错误:源文件 ${inputFile} 不存在`);process.exit(1);}console.log('正在清理HTML字符...');cleanHtml(inputFile);
}

step2: 运行

PS C:\Users\Administrator\WebstormProjects\untitled4> node file.js
正在清理HTML字符...
文件清理完成,已保存至:C:\Users\Administrator\Desktop\clean_file.txt
清理前字符数:2235
清理后字符数:1697
移除字符数:538

下面是用python实现同样的功能 C:\Users\Administrator\PycharmProjects\PythonProject2.venv\Scripts\activate_this.py

from pathlib import Path
import re
import sys# 定义文件路径
desktop_path = Path(r'C:\Users\Administrator\Desktop')
input_file = desktop_path / 'file.txt'
output_file = desktop_path / 'cleans_file.txt'# 自定义HTML实体替换规则
html_entities = {'&lt;': '<','&gt;': '>','&amp;': '&','&quot;': '"','&apos;': "'",'&#39;': "'",'&nbsp;': ' ',
}def clean_html(file_path):try:# 读取文件内容with open(file_path, 'r', encoding='utf-8') as f:content = f.read()# 分步骤清理内容cleaned = content# 移除HTML标签cleaned = re.sub(r'<[^>]+>', '', cleaned)# 替换HTML实体def replace_entity(match):entity = match.text.lower()return html_entities.get(entity, match.text)cleaned = re.sub(r'&(?:[a-z]+|#\d+);',replace_entity,cleaned,flags=re.IGNORECASE)# 清理多余换行和空格cleaned = re.sub(r'\n{3,}', '\n\n', cleaned)cleaned = re.sub(r' {2,}', ' ', cleaned)# 写入新文件with open(output_file, 'w', encoding='utf-8') as f:f.write(cleaned)print(f'文件清理完成,已保存至:{output_file}')print(f'清理前字符数:{len(content)}')print(f'清理后字符数:{len(cleaned)}')print(f'移除字符数:{len(content) - len(cleaned)}')except Exception as e:print('处理文件时发生错误:')print(f'{e}')sys.exit(1)if __name__ == "__main__":# 检查源文件是否存在if not input_file.exists():print(f'错误:源文件 {input_file} 不存在')sys.exit(1)print('正在清理HTML字符...')clean_html(input_file)

end

http://www.sczhlp.com/news/128811/

相关文章:

  • sns社区网站开发青岛公司网站建设公司排名
  • 军棋是哪个网站开发的网络服务器下载
  • 泸州大浪科技做网站企业网站分类举例
  • zen cart 创建的网站中国建设银行手机银行app下载
  • ftp 网站四川省建设网站电子签章
  • 改 187 个接口参数:Postman 卡壳时,Apipost 凭什么 5 分钟搞定?
  • 使用AWS Amplify、Lambda、API Gateway和DynamoDB部署静态Web应用
  • 无形资产 网站建设网站搭建h5是什么
  • 免费制作图片带字 手机软件保定网站seo费用
  • 求职网站开发多少钱微信视频制作小程序
  • 成都有哪些做网站的wordpress改变后台地址
  • 上海物流公司网站建设泰兴彩页设计
  • 宁波网站建设方案报价贵州网站建设推荐
  • 学了dw 就可以做网站了吗网站开发毕设论文
  • 龙华网页设计公司网站庆阳市西峰区做网站
  • 公司建设网站的公司网站怎么制作做
  • ui培训设计学校南宁网站seo公司
  • vscode的ssh-remote插件经常掉线
  • 网站模板编号超级单页网站模板
  • 百度 网站地图怎么做怎样做自己网站后台不被攻击
  • .gs域名做网站怎么样福建省住房建设厅网站6
  • php网站mysql数据库导入工具唐山哪个公司做网站
  • 网站建设费无形资产陕西网站建设公司哪有
  • 怎么做可以聊天的网站吗微信小程序app
  • 比较有名的网站建设公司天津网站制作费用
  • gg服务器租用网站新手建站广告联盟赚钱
  • 记录第一次CCPC(2025)网络赛前后
  • 第四周课前思考
  • 声像新境:东芝电视以火箭炮SOUND重塑家庭艺术馆新标准
  • wordpress 企业站教程网络整合营销案例