当前位置:网站首页>Perl转换文件的编码类型
Perl转换文件的编码类型
2022-06-30 19:44:00 【山音水月】
文章目录
有时会有对文件进行编码类型转换的需求,虽然目前的文本编辑软件大都提供了转换的功能,但批量转换还是不太方便。写了个小函数,能进行utf8->gbk或gbk->utf8的转换,如下
use strict;
use warnings;
use utf8;
use Encode;
sub convert_utf8_to_gbk;
sub convert_gbk_to_utf8;
#----------------------------------------------------------
# 主函数
#----------------------------------------------------------
my $dir_name = "deal_dir";
unless (-e $dir_name)
{
mkdir $dir_name;
}
my $file_path = "test.m";
convert_utf8_to_gbk($file_path,$dir_name);
#convert_gbk_to_utf8($file_path,$dir_name);
#----------------------------------------------------------
# 子函数
#----------------------------------------------------------
sub convert_utf8_to_gbk
{
my $utf8_file = shift;
my $dir_name = shift;
my $gbk_file = $dir_name."/".$utf8_file;
open my $file_rd,'<:encoding(utf8)',$utf8_file;
open my $file_wr,'>:encoding(gbk)',$gbk_file;
while(<$file_rd>)
{
chomp;
print $file_wr $_,"\n";
}
close $file_rd;
close $file_wr;
}
sub convert_gbk_to_utf8
{
my $gbk_file = shift;
my $dir_name = shift;
my $utf8_file = $dir_name."/".$gbk_file;
open my $file_rd,'<:encoding(gbk)',$gbk_file;
open my $file_wr,'>:encoding(utf8)',$utf8_file;
while(<$file_rd>)
{
chomp;
print $file_wr $_,"\n";
}
close $file_rd;
close $file_wr;
}
边栏推荐
- 【1175. 质数排列】
- Warmup预热学习率「建议收藏」
- Cartoon | has Oracle been abandoned by the new era?
- Filebeat custom indexes and fields
- Kubevela 1.4: make application delivery safer, easier to use, and more transparent
- WordPress 博客使用火山引擎 veImageX 进行静态资源 CDN 加速(免费)
- Django上传excel表格并将数据写入数据库的详细步骤
- Detailed explanation of specific methods and steps for TCP communication between s7-1500 PLCs (picture and text)
- Primary school, session 3 - afternoon: Web_ sessionlfi
- 盘点华为云GaussDB(for Redis)六大秒级能力
猜你喜欢

History, selection strategy and in-depth evaluation of note taking software

为什么数字化转型战略必须包括持续测试?

太湖 “中国健康农产品·手机直播万里行”走进太湖
Django上传excel表格并将数据写入数据库的详细步骤

数据智能——DTCC2022!中国数据库技术大会即将开幕

This morning, investors began to travel collectively

Idle fish is hard to turn over

网易云签到可抽奖?那一年我能签到365天。不信?你看。

Buttons to achieve various effects and functions. Reading this article is enough

GeoServer安装
随机推荐
Cartoon | has Oracle been abandoned by the new era?
为什么一定要从DevOps走向BizDevOps?
Build document editor based on slate
History, selection strategy and in-depth evaluation of note taking software
Warmup预热学习率「建议收藏」
Tupu software has passed CMMI5 certification| High authority and high-level certification in the international software field
Wechat applets - basics takes you to understand the life cycle of applets (2)
“更福特、更中国”拨云见日,长安福特王牌产品订单过万
2022 最新 JCR正式发布全球最新影响因子名单(前600名)
Smarter! Airiot accelerates the upgrading of energy conservation and emission reduction in the coal industry
【ICCV 2019】特征超分检测:Towards Precise Supervision of Feature Super-Resolution for Small Object Detection
CV+Deep Learning——网络架构Pytorch复现系列——basenets(BackBones)(一)
Playwright - 滚动条操作
条件编译
网易云签到可抽奖?那一年我能签到365天。不信?你看。
科大讯飞活跃竞赛汇总!(12个)
MySQL billing Statistics (Part 1): MySQL installation and client dbeaver connection
25:第三章:开发通行证服务:8:【注册/登录】接口:接收并校验“手机号和验证码”参数;(重点需要知道【利用redis来暂存数据,获取数据的】的应用场景)(使用到了【@Valid注解】参数校验)
【NLP】【TextCNN】 文本分类
Client请求外部接口标准处理方式