我尝试将.txt转换为.html格式。所以我尝试使用以下代码进行转换,并且还需要使用perl为表格格式的每一列提供不同的标题名称。
输入文件:input.txt
1:Explicit Placement blockages are created in the pad regions ?:Yes:INCORRECT:To Be Done
2:Explicit Routing blockages are created in the pad regions ?:Yes:INCORRECT:To Be Done
3:Is Complete bond pad meal is used for top level power hookup ?:Yes:INCORRECT:To Be Done
我试过的代码:
#!/usr/bin/perl
use strict;
use warnings;
open my $HTML, '>', 'output.html' or die $!;
print $HTML <<'_END_HEADER_';
<html>
<head><title></title></head>
<body>
_END_HEADER_
open my $IN, '<', 'input.txt' or die $!;
while (my $line = <$IN>) {
$convert=split(/\:/,$line);
print $HTML $convert;
}
print $HTML '</body></html>';
close $HTML or die $!;
预期产出:
第1列应根据序列号副打印每行的整个句子。即(在垫片区域中创建显式放置块)
|s.no|column1 |column2|column3 |column4 |
|1 |Explicit.... |yes |INCORRECT|To be done |
|2 |Explicit.... |yes |INCORRECT|To be done |
|1 |Is.......... |yes |INCORRECT|To be done |
答案 0 :(得分:4)
对代码进行最少的更改:
use strict;
use warnings;
open my $HTML, '>', 'output.html' or die $!;
print $HTML <<'_END_HEADER_';
<html>
<head><title></title></head>
<body>
<table>
_END_HEADER_
open my $IN, '<', 'input.txt' or die $!;
while (my $line = <$IN>) {
chomp $line;
print $HTML '<tr><td>' . join('</td><td>', split(/:/,$line)) . "</td></tr>\n";
#or
#print $HTML '<tr><td>' . $line =~ s|:|</td><td>|gr . "</td></tr>\n";
}
close $IN or die $!;
print $HTML <<'_END_FOOTER_';
</table>
</body>
</html>
_END_FOOTER_
close $HTML or die $!;
将生成以下html表:
<html>
<head><title></title></head>
<body>
<table>
<tr><td>1</td><td>Explicit Placement blockages are created in the pad regions ?</td><td>Yes</td><td>INCORRECT</td><td>To Be Done</td></tr>
<tr><td>2</td><td>Explicit Routing blockages are created in the pad regions ?</td><td>Yes</td><td>INCORRECT</td><td>To Be Done</td></tr>
<tr><td>3</td><td>Is Complete bond pad meal is used for top level power hookup ?</td><td>Yes</td><td>INCORRECT</td><td>To Be Done</td></tr>
</table>
</body>
</html>