当前位置:网站首页>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;
}
边栏推荐
- A necessary tool for testing -- postman practical tutorial
- 屏幕显示技术进化史
- 为什么一定要从DevOps走向BizDevOps?
- Warmup预热学习率「建议收藏」
- Primary school, session 3 - afternoon: Web_ xxe
- Character class of regular series
- MySQL billing Statistics (Part 1): MySQL installation and client dbeaver connection
- 软件工程最佳实践——项目需求分析
- Application of VoIP push in overseas audio and video services
- 广州股票开户选择手机办理安全吗?
猜你喜欢

昔日果汁大王,16个亿卖了

MySQL billing Statistics (Part 1): MySQL installation and client dbeaver connection

正则系列之字符类
![[multithreading] use the thread pool to implement a simple thread pool](/img/24/ad3ced1d4aebde3f2233b04752bead.png)
[multithreading] use the thread pool to implement a simple thread pool

The prospectus of pelt medical was "invalid" for the second time in the Hong Kong stock exchange, and the listing plan was substantially delayed
Detailed steps for Django to upload excel tables and write data to the database

How unity pulls one of multiple components

Tencent conference application market was officially launched, with more than 20 applications in the first batch

派尔特医疗在港交所招股书二次“失效”,上市计划实质性延迟

科大讯飞活跃竞赛汇总!(12个)
随机推荐
Ten percent of the time, the tar command can't parse the English bracket "()" when decompressing the file
太湖 “中国健康农产品·手机直播万里行”走进太湖
建立自己的网站(20)
“更福特、更中国”拨云见日,长安福特王牌产品订单过万
更智能!AIRIOT加速煤炭行业节能减排升级
Cv+deep learning network architecture pytoch recurrence series basenets (backbones) (I)
正则系列之字符类
dataloader 源码_DataLoader
Torchdrug -- drug attribute prediction
FH6908A负极关断同步整流模拟低压降二极管控制IC芯片TSOT23-6超低功耗整流器 1w功耗 <100uA静态 替代MP6908
pytorch实现FLOPs和Params的计算
英语没学好到底能不能做coder,别再纠结了先学起来
广州炒股开户选择手机办理安全吗?
Transport layer uses sliding window to realize flow control
将 EMQX Cloud 数据通过公网桥接到 AWS IoT
Spark - 一文搞懂 Partitioner
重复乃技艺之母
Filebeat自定义index和fields
RP prototype resource sharing - shopping app
CADD course learning (2) -- target crystal structure information