gpt4 book ai didi

arrays - 尝试使用PowerShell将CSV中的每一行和每一列乘以100或100

转载 作者:行者123 更新时间:2023-12-02 23:17:16 26 4
gpt4 key购买 nike

这是.txt示例的制表符分隔文件,它开始时是:

CH00-03 CH00-04 CH00-06 CH00-07 CH00-08
°F °F °F °F °F
1.1120e+002 1.0040e+002 4.0208e+002 4.4312e+002 4.5428e+002
2.3378e+002 2.3810e+002 2.4386e+002 2.5376e+002 4.1126e+002
2.6726e+002 2.5970e+002 2.5790e+002 2.5880e+002 2.6294e+002
-5.8662e+003 -5.8662e+003 -5.8662e+003 -5.8662e+003 -5.8662e+003

我正在运行以下代码,将其更改为CSV,同时也删除了E +:
$DataPath = 'C:\Test'
$DataName = '\*.txt'
$DataTemp = '\Tempfile.csv'
$NewData = '\Complete.csv'

$DataStream = $DataPath + $DataName
$TempStream = $Datapath + $DataTemp
$NewStream = $DataPath + $NewData
#$DataContent = $DataName

$DataContent = Get-Content -Path "$DataStream" -Raw

$Data1 = $DataContent -replace '[E+]',''
$Data2 = $Data1 #Required as for some reason no data comes out if not passed through another variable.
ECHO $Data2 > $TempStream

IMPORT-CSV -Delimiter "`t" -Header ('Column1','Column2','Column3','Column4','Column5') -Path $TempStream | Export-Csv $NewStream -NoType

然后我从$ NewStream抓取数据以将100或1000相乘,具体取决于删除E +后的最后一位数字

目前,我试图使乘法起作用,然后再选择乘以这里,这是一些试错法:
  • 测试以拆分数据和数字。
  • $import = get-content $NewStream
    $import | select -First 3 | Set-Content $Header
    $import | Select-Object -Skip 3 | Set-Content $CSVOUT
    $NewImport = Get-Content $CSVOUT
  • 尝试过不确定是否关闭。
  • $NewStream | foreach-object {$_ * 100} | Set-Content "C:\Test\Testing6.csv"
  • 尝试过此操作,但是由于某种原因,我觉得这无法与我得到的效果一起使用
  • $arr = $DataContent
    for($i = 0; $i -lt $arr.Length; $i++){
    $arr[$i] = $arr[$i] * 100
    }
    ECHO $arr > "C:\Test\Testing6.csv"

    也尝试了这个
    $arr = $DataContent2
    for($i = 0; $i -lt $arr.Length; $i++){
    $arr[$i] = [float]$arr[$i] *= 100
    ECHO $arr > "C:\Test\Testing6.csv"
    }

    $ NewStream看起来像这样
    Column1,Column2,Column3,Column4,Column5
    CH00-03,CH00-04,CH00-06,CH00-07,CH00-08
    °F,°F,°F,°F,°F
    1.1120002,1.0040002,4.0208002,4.4312002,4.5428002
    2.3378002,2.3810002,2.4386002,2.5376002,4.1126002
    2.6726002,2.5970002,2.5790002,2.5880002,2.6294002
    -5.8662003,-5.8662003,-5.8662003,-5.8662003,-5.8662003

    我试图根据每个数字的最后一个数字将所有这些乘以100或1000,但是无论我尝试如何,总是总是遇到不同的错误。

    我想我可能比需要做的要困难得多,请帮忙任何事情以使此工作正常进行。我也对其他Windows编程语言持开放态度:)

    编辑:
    谢谢Mathias,您已获得完成脚本所需的其余全部。
    “`t”似乎不起作用,但是如下所示,我找到了一种处理它的方法。

    这是对我有用的最终结果:
    #Setup to easily change Data Path.
    $DataPath = 'C:\Test'
    $DataName = '\*.txt'
    $DataTemp = '\Tempfile.csv'
    $NewData = '\Complete.csv'
    $FinalData = '\Final.csv'

    #Path Concatenation.
    $DataStream = $DataPath + $DataName
    $TempStream = $Datapath + $DataTemp
    $NewStream = $DataPath + $NewData
    $OUTCSV = $DataPath + $FinalData

    #Getting Content
    $DataContent1 = Get-Content -Path "$DataStream" -Raw

    #Because Get-Content does not work when introduced into IMPORT-CSV directly as a variable it needs a file changed to csv. all other ways would give me dupe header errors.
    $DataContent1 > $TempStream

    #Because "`t" does not work in Convertfrom-csv or Convertto-csv. Also because of Dupe header errors, this helps the most in eliminating them.

    IMPORT-CSV -Delimiter "`t" -Header ('Column1','Column2','Column3',
    'Column4','Column5','Column6','Column7','Column8','Column9','Column10',
    'Column11','Column12','Column13','Column14','Column15','Column16',
    'Column17','Column18','Column19','Column20','Column21','Column22',
    'Column23','Column24','Column25','Column26','Column27','Column28',
    'Column29','Column30') -Path $TempStream | Export-Csv $NewStream -NoType

    #Grabbing the new content.
    $DataContent2 = Get-Content -Path "$NewStream" -Raw

    #This is the Scientific Notation conversion to Decimal.
    $DataContent2 = [regex]::Replace($DataContent2, '(-?\d+\.\d+e\+\d+)', {param($m) $m.Value -as [double]})

    #This creates the File I needed:)
    $DataContent2|ConvertFrom-Csv -delimiter ","|Export-Csv -Path $OUTCSV -NoTypeInformation

    #To delete the CSV files used to build the Final CSV

    DEL $TempStream
    DEL $NewStream

    最佳答案

    您无需手动解析这些数字,PowerShell支持科学计数法!

    PS C:\> '4.0208e+002' -as [double]
    402.08

    因此,您要做的就是将值转换为数字类型!您可以使用 Regex.Replace()方法传递一个脚本块,该脚本块以科学计数形式转换数字,如下所示:

    $DataContent = Get-Content -Path  "$DataStream" -Raw

    $DataContent = [regex]::Replace($DataContent, '(-?\d+\.\d+e\+\d+)', {param($m) $m.Value -as [double]})

    现在,您发布的示例输入的 $DataContent如下所示:
    CH00-03 CH00-04 CH00-06 CH00-07 CH00-08
    °F °F °F °F °F
    111.2 100.4 402.08 443.12 454.28
    233.78 238.1 243.86 253.76 411.26
    267.26 259.7 257.9 258.8 262.94
    -5866.2 -5866.2 -5866.2 -5866.2 -5866.2

    现在,只需将TSV转换为对象并使用默认的定界符再次导出为CSV,您便会拥有所需的格式:

    $DataContent |ConvertFrom-Csv -Delimiter "`t" |Export-Csv -Path $OUTCSV -NoTypeInformation

    关于arrays - 尝试使用PowerShell将CSV中的每一行和每一列乘以100或100,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62076675/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com