(个人)读取A.CSV修改它的某列,写入B.CSV

#!/usr/bin/perl -w
use strict;
use warnings;
use Tie::File;

open(IN_FILE,"<E:/Hzj_works/test1.csv");

my $filename = "E:/Hzj_works/test1.csv";
tie my @array, 'Tie::File', $filename or die "$!";

if ( @array ) {
print "$filename have file. \n";
open(my $OT_FILE,">E:/Hzj_works/test2.csv");
while(my $line = <IN_FILE>){
my @fields = split "," , $line;
my @table;
my $tag1 = ",";
push(@table,"$fields[0]$tag1");

#if $fields[1]
my @timetemp = split ":" , $fields[1];
my $len = length($timetemp[0]);
my $hour;
my $min;
my $zero = "0"; #补0
if($len == 1){
$hour = "$zero$timetemp[0]";
}else{
$hour = $timetemp[0];
}
my $mint = substr($timetemp[1],0,1);
$min = "$mint$zero";
my $newtime = "$hour$mint$zero"; #变换后的时间
push(@table,"$newtime$tag1");

#假定 $fields[2]
my $mini = substr($fields[2],14,2); #获取分钟
my $minite = 30;#用于跟获取的时间比较
my $time1;
if($mini <= $minite){
my $mini1 = substr($fields[2],0,14);
my $mini2 = "30:00";
$time1 = "$mini1$mini2";
}else{
my $mini1 = substr($fields[2],0,14);
my $mini2 = "00:00";
#小时加1
my $hour1 = substr($fields[2],11,2);
my $hour2 = $hour1 + 1;
my $hour3 = substr($fields[2],0,11);
if($hour2 == 24){
$hour2 = "00";
}
my $tag = ":";
$time1 = "$hour3$hour2$tag$mini2";
}

push(@table, "$time1$tag1", "$fields[3]$tag1", $fields[4]);
print @table;
print $OT_FILE $_ for @table;
}
}
else {
print "$filename is empty. \n";
exit;
}

close(IN_FILE);

上一篇:【数据库-MySql】开启事件 event_scheduler


下一篇:进程Process之join、daemon(守护)、terminate(关闭)、multiprocessing之锁、信号量和事件