使用Powershell将CSV转换为特定XML
我正在尝试使用powershell将csv转换为xml表。有没有一种方法可以精确地指定xml将输出什么使用Powershell将CSV转换为特定XML,xml,powershell,csv,Xml,Powershell,Csv,我正在尝试使用powershell将csv转换为xml表。有没有一种方法可以精确地指定xml将输出什么 Example CSV input: TIME,TEMP,HUMID,DEWPT "03/07/2012 12:04:59",72.1,73.2,62.5 "03/07/2012 11:34:53",71.9,73.8,62.5 "03/07/2012 11:04:46",71.8,74.6,62.7 "03/07/2012 10:34:39",72.3,72.6,62.4 "03/07/2
Example CSV input:
TIME,TEMP,HUMID,DEWPT
"03/07/2012 12:04:59",72.1,73.2,62.5
"03/07/2012 11:34:53",71.9,73.8,62.5
"03/07/2012 11:04:46",71.8,74.6,62.7
"03/07/2012 10:34:39",72.3,72.6,62.4
"03/07/2012 10:04:33",71.9,72.1,61.9
Example XML output:
<?xml version="1.0" encoding="UTF-8" ?>
- <chart>
- <series>
<value xid="0">03/07/2012 00:02:06</value>
<value xid="1">03/07/2012 00:32:17</value>
<value xid="2">03/07/2012 01:02:26</value>
<value xid="3">03/07/2012 01:32:35</value>
<value xid="4">03/07/2012 02:02:42</value>
</series>
- <graphs>
- <graph gid="0" color="#32CD32" axis="right" title="Temperature">
<value xid="0">43.1</value>
<value xid="1">44.0</value>
<value xid="2">43.6</value>
<value xid="3">43.1</value>
<value xid="4">42.7</value>
</graph>
- <graph gid="1" color="#FFD700" axis="left" title="Humidity">
<value xid="0">64.6</value>
<value xid="1">100.0</value>
<value xid="2">57.7</value>
<value xid="3">71.6</value>
<value xid="4">44.3</value>
</graph>
<graph gid="2" color="#8B008B" axis="left" title="Dewpoint">
<value xid="0">30.4</value>
<value xid="1">44</value>
<value xid="2">28.4</value>
<value xid="3">32.9</value>
<value xid="4">22.6</value>
</graph>
</graphs>
</chart>
示例CSV输入:
时间、温度、湿度、露点
"03/07/2012 12:04:59",72.1,73.2,62.5
"03/07/2012 11:34:53",71.9,73.8,62.5
"03/07/2012 11:04:46",71.8,74.6,62.7
"03/07/2012 10:34:39",72.3,72.6,62.4
"03/07/2012 10:04:33",71.9,72.1,61.9
XML输出示例:
-
-
03/07/2012 00:02:06
03/07/2012 00:32:17
03/07/2012 01:02:26
03/07/2012 01:32:35
03/07/2012 02:02:42
-
-
43.1
44
43.6
43.1
42.7
-
64.6
100
57.7
71.6
44.3
30.4
44
28.4
32.9
22.6
数据可能不是100%一致,我没有从同一个地方抓取每个样本,但你应该得到漂移。这样的事情可能吗
还有。。。我保持一个大型的主csv。每小时有两条记录被添加到其中。是否可以像这样巧妙地增加csv到xml的增量,或者每次都必须针对主csv运行脚本。我知道它将运行在主csv上,但我担心随着时间的推移,随着列表的不断增长,它将花费越来越长的时间。如果我能够只添加新条目,那将使它运行得更快。这有效吗
$head = @'
<?xml version="1.0" encoding="UTF-8" ?>
<chart>
<series>
'@
$series = @'
'@
$temp_graph = @'
</series>
<graphs>
<graph gid="0" color="#32CD32" axis="right" title="Temperature">
'@
$humid_graph = @'
<graph gid="1" color="#FFD700" axis="left" title="Humidity">
'@
$dewpt_graph = @'
<graph gid="2" color="#8B008B" axis="left" title="Dewpoint">
'@
$end = @'
</graphs>
</chart>
'@
$i = 0
import-csv file.csv |
foreach-object {
$series += @"
<value xid="$i">$($_.TIME)</value>
"@
$temp_graph += @"
<value xid="$i">$($_.TEMP)</value>
"@
$humid_graph += @"
<value xid="$i">$($_.HUMID)</value>
"@
$dewpt_graph += @"
<value xid="$i">$($_.DEWPT)</value>
"@
$i++
}
$temp_graph,$humid_graph,$dewpt_graph | foreach {$_ += '<\graph>'
$head + $series + $temp_graph + $humid_graph + $dewpt_graph + $end
$head=@'
'@
$series=@'
'@
$temp_graph=@'
'@
$U图形=@'
'@
$dewpt_图形=@'
'@
$end=@'
'@
$i=0
导入csv文件.csv|
foreach对象{
$series+=@”
$($次)
"@
$temp_图形+=@”
$($温度)
"@
$U图形+=@”
$($湿)
"@
$dewpt_图形+=@”
$($。DEWPT)
"@
$i++
}
$temp_图、$MEDIM_图、$dewpt_图| foreach{$\u+=''
$head+$series+$temp\u graph+$humid\u graph+$dewpt\u graph+$end
以下脚本以增量方式将数据添加到xml(使用regex和replace功能)。csv文件input.csv
应仅包含新条目
# filename: test.ps1
function Get-ScriptDirectory
{
$Invocation = (Get-Variable MyInvocation -Scope 1).Value
Split-Path $Invocation.MyCommand.Path
}
$csvpath = join-path (get-scriptdirectory) input.csv
$xmlpath = join-path (get-scriptdirectory) output.xml
if (!(test-path $xmlpath)) {
@"
<?xml version="1.0" encoding="UTF-8" ?>
<chart>
<series>
</series>
<graphs>
<graph gid="0" color="#32CD32" axis="right" title="Temperature">
</graph>
<graph gid="1" color="#FFD700" axis="left" title="Humidity">
</graph>
<graph gid="2" color="#8B008B" axis="left" title="Dewpoint">
</graph>
</graphs>
</chart>
"@ | out-file -encoding "UTF8" $xmlpath
}
$xml = [System.IO.File]::ReadAllText($xmlpath)
$data = import-csv $csvpath
$counter = [int](([regex]‘xid="(\d*)"’).matches($xml) | foreach {$_.Groups[1].Value} | %{[int] $_} | sort | select -last 1)
$series = $null;$temp_graph = $null;$humid_graph = $null;$dewpt_graph = $null
$data | %{
$counter++
$series += @"
<value xid="$counter">$($_.TIME)</value>
"@
$temp_graph += @"
<value xid="$counter">$($_.TEMP)</value>
"@
$humid_graph += @"
<value xid="$counter">$($_.HUMID)</value>
"@
$dewpt_graph += @"
<value xid="$counter">$($_.DEWPT)</value>
"@
}
$xml = $xml -replace "</series>","$series</series>"
$xml = $xml -replace "</graph>\r*\n*\s*<graph gid=`"1`"","$temp_graph</graph><graph gid=`"1`""
$xml = $xml -replace "</graph>\r*\n*\s*<graph gid=`"2`"","$humid_graph</graph><graph gid=`"2`""
$xml = $xml -replace "</graph>\r*\n*\s*</graphs>","$dewpt_graph</graph></graphs>"
$xml | out-file -encoding "UTF8" $xmlpath
#文件名:test.ps1
函数获取脚本目录
{
$Invocation=(获取变量MyInvocation-Scope 1).Value
拆分路径$Invocation.MyCommand.Path
}
$csvpath=加入路径(获取脚本目录)input.csv
$xmlpath=连接路径(获取脚本目录)output.xml
if(!(测试路径$xmlpath)){
@"
“@| out文件-编码为“UTF8”$xmlpath
}
$xml=[System.IO.File]::ReadAllText($xmlpath)
$data=导入csv$csvpath
$counter=[int](([regex]'xid=“(\d*)”).matches($xml)| foreach{$|.Groups[1].Value}{[int]$|}| sort | select-last 1)
$series=$null;$temp\U graph=$null;$MEDIM\U graph=$null;$dewpt\U graph=$null
$data|%{
美元柜台++
$series+=@”
$($次)
"@
$temp_图形+=@”
$($温度)
"@
$U图形+=@”
$($湿)
"@
$dewpt_图形+=@”
$($。DEWPT)
"@
}
$xml=$xml-替换“”“$series”
$xml=$xml-replace“\r*\n*\s*我在尝试此代码时遇到错误:从第1行开始的字符串char:9+$head=Copy/pass error。终止“@和”@一定是这行的开头。你是对的。一旦我修复了它,它就工作得很好。谢谢你的帮助。我发现了另一个问题。Temp和潮湿上没有结束标记。我尝试将它们添加到脚本中,得到了奇怪的格式。我尝试在下一节的开头添加它。我用原始脚本和它在一台机器上工作,在另一台机器上出现问题。它遇到问题的是一个较旧版本的powershell。这会有什么不同吗?还有,你发布的新脚本。在这两台机器上都没有任何运气,添加了。但即使没有添加,它在开始时仍然存在问题。要重新访问吗s在周一。可能必须使用具有早期版本的powershell的机器。这类工作正常。它增量添加了时间和露点条目,但湿度和温度没有更新。