替换CSV文件中的字段 [英] replacing a field in a csv file

查看:211
本文介绍了替换CSV文件中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是perl的新手.我需要一个代码来读取csv文件并将第5个字段替换为一个值.

例如.在csv文件中,如果第5个字段是"C",则将其替换为1,否则将其替换为2.

输出文件必须是其他文件.

我已经为此编写了代码.谁能告诉我它是否正确.

Hi,

I am new to perl. I need a code to read a csv file and replace the 5th field by a value.

E.g. in the csv file if the 5th field is ''C'' , then replace it as 1 else replace with 2.

The output file must be a different file.

I have written a code for it. Can anyone tell me if its correct or not.

#!/usr/bin/perl -w
$arg1=$ARGV[0];
$arg2=$ARGV[1];
open INP,"<" ,$arg1;
open OUT,">" ,$arg2;

while($line=<inp>)
{
    @arr=split(/,/,$line,5);
    if (arr[4] eq ''C'')
    {
         arr[4]=1;
    }
    else
    {
         arr[4]=2;
    }
      $str=join('','',@arr);
     print OUT $str;
}

close INP;
close OUT;</inp>




请帮助我一个人..非常紧急.
如果不正确,请为其编写替代代码.

谢谢

Deepak




Please help me someone.. Its very urgent.
If its not correct then please write an alternative code for it.

Thanks

Deepak

推荐答案

arg1 =


ARGV [0];
ARGV[0];


arg2 =


这篇关于替换CSV文件中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆