如何使用perl将.txt文件内容转换为.html格式? [英] How to convert .txt file contents into .html format using perl?
本文介绍了如何使用perl将.txt文件内容转换为.html格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试将.txt转换为.html格式。所以我尝试使用以下代码进行转换,并且还需要使用perl为表格格式的每一列提供不同的标题名称。
I tried to convert .txt into .html format.So i tried the following code to convert and also need to give different heading names for every column in table format using 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列应该是根据序列号副本打印每行的整个句子。即(在垫片区域中创建显式放置块)
Column 1 should print the entire sentence for every line according to serial number vice. i.e from (Explicit Placement blockages are created in the pad regions)
|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 |
推荐答案
只需对代码进行最少的更改:
With minimal changes to your code:
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表:
will produce the following html-table:
<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>
这篇关于如何使用perl将.txt文件内容转换为.html格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文