perl的文本替换功能
发布时间:2020-12-16 00:32:06 所属栏目:大数据 来源:网络整理
导读:文件一: ?xml version="1.0" encoding="UTF-16"?StringTable version ='0' Language name ='English' String _locID ='18103 ?symbol='cStringPlaceProblemCost''New Traps and Weapons/String String _locID ='18104'Levels/String String _locID ='19000'
文件一:
<?xml version="1.0" encoding="UTF-16"?> <StringTable version ='0'> <Language name ='English'> <String _locID ='18103 ?symbol='cStringPlaceProblemCost''>New Traps and Weapons</String> <String _locID ='18104'>Levels</String> <String _locID ='19000'>Bladestaff</String> <String _locID ='19001'>Holy Blade</String> <String _locID ='19002 ?symbol='cStringPlaceProblemCost''>Attack the enemy up close</String> <String _locID ='19003'>Smite the enemy up close</String> </Language> </StringTable> 文件二: <String> <Id>19000</Id> <ENG>Bladestaff</ENG> <CHS>锋利法杖</CHS> </String> <String> <Id>19001</Id> <ENG>Holy Blade</ENG> <CHS>圣剑</CHS> </String> <String> <Id>19002</Id> <ENG>Attack the enemy up close</ENG> <CHS>近距离攻击敌人</CHS> </String> <String> <Id>19003</Id> <ENG>Smite the enemy up close</ENG> <CHS>近距离重击敌人</CHS> </String>?任务要求,将文件二中的中文翻译写入到文件一替换英文。变成如下内容: <?xml version="1.0" encoding="UTF-16"?> <StringTable version ='0'> <Language name ='English'> <String _locID ='18103' ?symbol='cStringPlaceProblemCost'>New Traps and Weapons</String> <String _locID ='18104'>Levels</String> <String _locID ='19000'>锋利法杖</String> <String _locID ='19001'>圣剑</String> <String _locID ='19002 ?symbol='cStringPlaceProblemCost''>近距离攻击敌人</String> <String _locID ='19003'>近距离重击敌人</String> </Language> </StringTable>
所以我很明智的选择了Perl,代码如下: #!/bin/perl #编码设置 #use encoding 'utf8',STDIN=>'utf8',STDOUT=>'gb2312'; #判断参数个数 ##################################################### #$numargs = @ARGV; #if($numarges == 2) #{ # print STDERR ("参数个数不对.n"); # exit(1); #} #################################################### $MyXmlName = "MyFile.xml"; #自定义XML文件 $ARGV[0] $GameFileName = "StringTable.xml"; #游戏XML文件 $ARGV[1] $OutFileName = "My_$GameFileName"; #将游戏内的XML数据修改为中文 if (!-e $GameFileName) { #文件不存在则退出 print STDERR ("File $GameFileName not exists.n"); exit(1); } #打开游戏XML数据文件 unless (open(INFILE,"<$GameFileName")) { die ("cannot open input file $GameFileNamen"); } #打开写入文件 unless (open(OUTFILE,">$OutFileName")) { die ("cannot open output file $OutFileNamen"); } #打开自定义XML文件 if (!-e $MyXmlName) { #文件不存在则退出 print STDERR ("File $MyXmlName not exists.n"); exit(1); } unless (open(MYFILE,"<$MyXmlName")) { die ("cannot open input file $MyXmlNamen"); } # %hash = (); #(2=>"string",1=>"file"); #形成 字符ID -- 字符串 的 hash while($string = <MYFILE>) { if($string =~ /<String>/) #只有存在</String>的字符串才进入替换,否者直接写入文件 { $string = <MYFILE>; #读取 ID 行 @words = split(/[<>]/,$string);#用<>分隔字符串 $id = $words[2]; $string = <MYFILE>; #读取 ENG 行 $string = <MYFILE>; #读取 CHS 行 @words = split(/[<>]/,$string);#用<>分隔字符串 $cnstr = $words[2]; if ( $cnstr =~ "[rn]+") #如果没有中文翻译的行,会有rn { } else { $hash{$id} = $cnstr;#加入Hash表 } } } #文件修改部分 while($string = <INFILE>) { if($string =~ /</String>/) #只有存在</String>的字符串才进入替换,否者直接写入文件 { @id = split(/['']/,$string);#用'分隔字符串 $string_id = $id[1]; #$id[1]为id值 @words = split(/[<>]/,$string);#用<>分隔字符串 if( defined $hash{$string_id} ) #判断Hash中是否存在此值 { #直接将结果写入文件 print OUTFILE (" ","<$words[1]>$hash{$string_id}<$words[3]>n");#其中$word[2]位因为字符串 } else { print OUTFILE ($string);#hash中不存在的翻译项,直接原样输出 } } else { print OUTFILE ("$string");#没有需要翻译的字符串 } } #关闭文件 close(INFILE); close(OUTFILE); close(MYFILE); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |