首页 > 解决方案 > 将数据从文本文件转换为 html

问题描述

我正在为这个 powershell 类完成我的最后一项重大任务,我正在努力找出正则表达式的正确用法。基本上,我一直在尝试正确导出 html 文件并正确使用学生 ID。再次感谢您对此的任何帮助。谢谢!

说明:使用 PowerShell ISE 或 VSCode,创建将执行以下操作的 PowerShell 脚本:

您将从标准处理报告创建 HTML 网页报告。您将使用位于 Canvas 上的文本文件“STUDENTS.TXTP 预览文档”。

脚本必须满足以下要求:

输出文件应命名为 ITS3410-{您的姓名}.htm 报告中必须包含具有学生编号 (9999-9999) 的每一行数据 所有数据必须修剪 无前导空格 无尾随空格 无空值没有垃圾线 必须使用正则表达式 (regex) 检测学号 您必须在代码中包含注释以解释它 报告标题必须在 HTML 网页上可见

########SCRIPT######################
$inputFile = Get-Content c:\powershell\STUDENTS.txt    #getting input file
$output_file = 'c:\powershell\ITS3410-myname.htm'       #to get the output file


$FileLine = @()
Foreach ($Line in $inputFile) {
$MyObject = New-Object -TypeName PSObject
#adding the objects
Add-Member -InputObject $MyObject -Type NoteProperty -Name Student# -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name LAST -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name FIRST -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name DN -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name ERN -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name HR -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name STS -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name DEG -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name CON -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name VER -Value $Line
Add-Member -InputObject $MyObject -Type NoteProperty -Name GPA -Value $Line

$FileLine += $MyObject
}
#to convert the script into html
$FileLine | ConvertTo-Html -Property Student#, LAST, FIRST, DN, ERN, HR, STS, DEG, CON, VER, GPA - 
body "<H2>Student Report</H2>" | Out-File $output_file

Invoke-Expression $output_file
$line = "9999-9999  Denty Bryon N  72 8     4       CER CSC      2006A          3.43"
$strings = $line -split " " | Where-Object { $_ }
# Bulk add properties
$props = @{
               StudentID = $strings[0]
               Surname =   $strings[1]
               Firstname = $strings[2]
               DN = $strings[3]
               ERN = $strings[4]
               HR = $strings[5]
               STS = $strings[6]
               DEG = $strings[7]
               CON = $strings[8]
}

# Add more properties by name
$props["VER"] = $strings[9]
$props["GPA"] = $strings[10]

标签: powershell

解决方案


假脱机警报:

如果下面的建议有效,您应该尝试通过逆向工程来理解解决方案。

根据您提供的脚本,我猜您的输入数据如下所示:

$inputFile = '
Student#   LAST  FIRST DN ERN HR STS DEG  CON  VER   GPA
9999-9999  Denty Bryon N  72  8  4   CER  CSC  2006A 3.43
1234-5678  Doe   John  D  65  7  5   CER2 CSC2 2006B 1.23
'

使用此ConvertFrom-SourceDatacmdlet:

Get-Content .\STUDENTS.txt | ConvertFrom-SourceTable | ConvertTo-Html | Set-Content .\ITS3410-myname.htm

结果:

HTML 视图

$inputFile | ConvertFrom-SourceTable | ConvertTo-Html 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HTML TABLE</title>
</head><body>
<table>
<colgroup><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/><col/></colgroup>
<tr><th>Student#</th><th>LAST</th><th>FIRST</th><th>DN</th><th>ERN</th><th>HR</th><th>STS</th><th>DEG</th><th>CON</th><th>VER</th><th>GPA</th></tr>
<tr><td>9999-9999</td><td>Denty</td><td>Bryon</td><td>N</td><td>72</td><td>8</td><td>4</td><td>CER</td><td>CSC</td><td>2006A</td><td>3.43</td></tr>
<tr><td>1234-5678</td><td>Doe</td><td>John</td><td>D</td><td>65</td><td>7</td><td>5</td><td>CER2</td><td>CSC2</td><td>2006B</td><td>1.23</td></tr>
</table>
</body></html>

相关问题/其他方法


推荐阅读