簡體   English   中英

如何使用 Powershell 將帶有元數據標題行的 CSV 文件轉換為平面表格?

[英]How can I convert CSV files with a meta data header row into flat tables using Powershell?

我有幾千個格式與此類似的 CSV 文件(即頂部帶有元數據行的表格):

dinosaur.csv,water,Benjamin.Field.12.Location53.Readings,
DATE,VALUE,QUALITY,STATE
2018-06-01,73.83,Good,0
2018-06-02,45.53,Good,0
2018-06-03,89.123,Good,0

是否可以使用 PowerShell 將這些 CSV 文件轉換為這樣的簡單表格格式?

DATE,VALUE,QUALITY,STATE,FILENAME,PRODUCT,TAG
2018-06-01,73.83,Good,0,dinosaur.csv,water,Benjamin.Field.12.Location53.Readings
2018-06-02,45.53,Good,0,dinosaur.csv,water,Benjamin.Field.12.Location53.Readings
2018-06-03,89.123,Good,0,dinosaur.csv,water,Benjamin.Field.12.Location53.Readings

或者是否有更好的替代方法來將這些 CSV 准備為可攝取的直接格式?

我之前使用 PS 處理過簡單的 CSV,但沒有使用重要的元數據行。

謝謝

    ## If your inital block is an accurate representation
$s = get-content .\test.txt

## Get the 'metadata' line
$metaline = $s[0]

## Remove the metadata line from the original and turn it into a custom powershell object
$n = $s | where-object { $_ -ne $metaline } | ConvertFrom-Csv

## Split the metadata line by a comma to get the different parts for appending to the other content
$m = $metaline.Split(',')

## Loop through each item  and append the metadata information to each entry
for ($i=0; $i -lt $n.Count; $i++) {
    $n[$i] = $n[$i] | Select-Object -Property *,FILENAME,PRODUCT,TAG  ## This is a cheap way to create new properties on an object
    $n[$i].Filename = $m[0]
    $n[$i].Product  = $m[1]
    $n[$i].Tag      = $m[2]
}

## Display that the new objects reports as the desired output
$n | format-table

注意:這是thepip3r 的有用答案更快替代方法,並且還涵蓋了將修改后的內容保存回 CSV 文件的方面:

通過使用switch語句以文本形式高效地循環文件行,可以避免對ConvertFrom-CsvSelect-ObjectExport-Csv的昂貴調用。

請注意, switch語句包含在$()子表達式 operator ,以便能夠在單個管道中寫回同一文件; 但是,這樣做需要將整個(修改過的)文件保存在內存中; 如果這不是一個選項,請將switch語句括在& { ... }並將其通過管道傳遞到Set-Content到一個臨時文件,您可以稍后使用該文件替換原始文件。

# Create a sample CSV file in the current dir.
@'
dinosaur.csv,water,Benjamin.Field.12.Location53.Readings,
DATE,VALUE,QUALITY,STATE
2018-06-01,73.83,Good,0
2018-06-02,45.53,Good,0
2018-06-03,89.123,Good,0
'@ > sample.csv

# Loop over all *.csv files in the current dir.
foreach ($csvFile in Get-Item *.csv) {

  $ndx = 0
  $(
    switch -File $csvFile.FullName {
      default {
        if ($ndx -eq 0) {  # 1st line
          $suffix = $_ -replace ',$' # save the suffix to append to data rows later
        } elseif ($ndx -eq 1) {  # header row
          $_ + ',FILENAME,PRODUCT,TAG' # add additional column headers
        } else { # data rows
          $_ + ',' + $suffix  # append suffix
        }
        ++$ndx
      }
    }
  ) # | Set-Content $csvFile.FullName  # <- activate this to write back to the same file.
    # Use -Encoding as needed.

}

以上產生以下結果:

DATE,VALUE,QUALITY,STATE,FILENAME,PRODUCT,TAG
2018-06-01,73.83,Good,0,dinosaur.csv,water,Benjamin.Field.12.Location53.Readings
2018-06-02,45.53,Good,0,dinosaur.csv,water,Benjamin.Field.12.Location53.Readings
2018-06-03,89.123,Good,0,dinosaur.csv,water,Benjamin.Field.12.Location53.Readings

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM