【问题标题】:How to browse a csv file, when the header line includes a null or empty value?当标题行包含空值或空值时,如何浏览 csv 文件?
【发布时间】:2021-11-30 07:47:48
【问题描述】:

我有一个包含 10 列的 A.csv 文件,除了最后一列之外,所有列都有一个名称。
我想生成一个带有标题"ID", "Type", "Name", "Documentation" 的新 D.csv 文件。
刚开始工作时,我给这个空列起了个名字,因为我不知道如何读取 powershell 中空列的值。 "TechnologyEvent"这种类型我不能做,请你帮帮我?

fichier A.Csv

Jobstream,Jobstream Description,Op num, Job,Script or expected file(s), Server, user,location,Job Description,  
PAXCWEBX965H,"RMNF - xWEBX1","9","","technical","","","","Begin Of JobStream","05h00"
PAXCWEBX965H,"RMNF - xWEBX1","40","PWEBX0GJ","PWEBX-965H-005S.KSH","PRAXCWBXLBDD01","svc_bddo_user","F+WEBX-027","Lancement Sauv. RMAN","05h00"

Fichier D.csv

ID,"Type","Name","Documentation"
79570056-ab4f-6969-8c58-a5bd5847a895,"TechnologyInteraction","WEBX965H","RMNF - xWEBX1"
b57a5fff-9dd2-4cm1-9141-04c042f49498,"TechnologyService","WEBX965H-005S","Lancement Sauv. RMAN"
id-bdca3a1b39ed49cd80ae41deaa88094c,"TechnologyEvent","Lancement Sauv. RMAN  05:00",""
function newElements{

    process{
    
        # import data 
    $data = Import-csv -Path $env:USERPROFILE\A.csv -Delimiter ';'

        $NewExtract_AGRe_TWS_ALL_20200925= ForEach($Entry in $ElementCsv){
  

 
    # -or [string]::IsNullOrEmpty($Entry."Script or expected file(s)")
    if ( ($Entry."Script or expected file(s)" -ilike 'technical') -or [string]::IsNullOrEmpty($Entry."Script or expected file(s)")  ) {
        $Entry."Jobstream"=$Entry."Jobstream" -replace '^(?:PAXC)?(.+?)','$1' 

    }

    else {
        $Entry."Jobstream" = $Entry.'Script or expected file(s)' -replace '^(?:P|A|X|C)?(.+?)(\.(?:BAT|KSH)|$)','$1' -replace '^(.{4})-','$1'
    }

        $Entry
    }
        # Export Extract_AGRe_TWS_ALL_20211005.csv  in new B.csv file 
        $NewExtract_AGRe_TWS_ALL_20200925  | Export-Csv $env:USERPROFILE\B.csv -NoTypeInformation -Encoding UTF8
    
        $NewExtract_AGRe_TWS_ALL_20200925Jobstream= Import-Csv $env:USERPROFILE\B.csv | Where { $oldElementsCsv.Name -notcontains $_.Jobstream}| Export-Csv $env:USERPROFILE\C.csv -NoTypeInformation  -Encoding UTF8 
        $ImportCsv=Import-Csv $env:USERPROFILE\C.csv
        #Output progress:
        $output= @()
        #Jobstream, Jobstream Description,Op num, Job,Script or expected file(s),Server, user, location, Job Description
        ForEach ($column in $ImportCsv){
        $pattern='^(.*)-'
        $pattern2='^(.*)_'
    if($column.Jobstream  -notmatch $pattern){

        $output1= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyInteraction"; "Name"= $column.Jobstream; "Documentation"=$column."Job Description" + " `r`nSever: $($column.Server)  `r`nuser: $($column.user) " ;  "Planification"= $column.Planification }   
        Write-Warning "Found new Jobstream : $($column.Jobstream)"
        Write-Warning "Found new Jobstream Description : $($column."Job Description")"
        $output= $output + $output1
    }  

  
          elseif($column.Jobstream  -match $pattern2) 
    {

        $output2= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyService"; "Name"=$column.Jobstream; "Documentation"= $column."Job Description"+ " `r`nSever: $($column.Server)  `r`nuser: $($column.user) " ; "Planification"= $column.Planification   } 
        Write-Warning "New Jobstream : $($column.Jobstream)"
        Write-Warning "New Jobstream Description : $($column."Job Description")"
        $output= $output + $output2

    } 
   elseif (![string]::IsNullOrEmpty($column.Planification) ){

        $output4= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyEvent"; "Name"= $column."Job Description" + " " + $column.Planification ; "Documentation"= ""  } 

        Write-Warning "New Type TechnologyEvent : $( $column."Job Description")"
        Write-Warning "New Type TechnologyEvent Description : $($column."Job Description")"
        $output= $output + $output4

    }
    else{

        $output3= New-Object PsObject -Property @{"ID"=[guid]::NewGuid().ToString(); "Type"="TechnologyService"; "Name"= $column.Jobstream; "Documentation"= $column."Job Description" + " " + $column.Planification + " `r`nSever: $($column.Server)  `r`nuser: $($column.user)" } 

        Write-Warning "New Jobstream : $($column.Jobstream)"
        Write-Warning "New Jobstream Description : $($column."Job Description")"
        $output= $output + $output3
    }

 

    }
    $output |Select-Object -Property "ID","Type","Name","Documentation" -Unique| Export-Csv $path\D.csv -NoTypeInformation -Encoding UTF8 

    #open new File:
    notepad $path\D.csv
    Write-Host "Ending newElements "
    }
}newElements

【问题讨论】:

  • 你的缩进太可怕了。请修复它,以便清楚地知道 foreach、if、elseif 等何时开始和结束。然后,示例中的 csv 文件使用 comma 作为分隔符,因此如果您的真实文件是这种情况,请删除 -Delimiter ';'
  • “我给这个空列起了个名字” - 怎么样?在哪里?什么空栏?如果您想要一个包含 "ID","Type","Name","Documentation" 列的 CSV 文件,您应该使用这四个属性名称创建 powershell 对象并将对象通过管道传输到 Export-CsvConvertTo-Csv
  • 我没有将其作为列:Jobstream,Jobstream Description,Op num, Job,Script or expected file(s), Server, user,location,Job Description, 我将其转换为:Jobstream,Jobstream描述、操作编号、作业、脚本或预期文件、服务器、用户、位置、作业描述、计划
  • 我把它变成了手写的

标签: windows powershell csv


【解决方案1】:

您可以在使用-Headers 导入 CSV 时指定标题:

# I set the 10th column name to "Schedule".
# You need to skip the first row if the file contains a header already:
$header = ('Jobstream','StreamDescription','OpNum','Job','Script','Server','user','location','JobDescription','Schedule')
$data = Import-csv -Path $env:USERPROFILE\A.csv -Delimiter ';' -Header $header |
  Select -Skip 1

要创建自定义 csv,请尝试使用以下内容:

$output = ForEach ($row in $ImportCsv) {
  [PSCustomObject][ordered]@{
    "ID"=[guid]::NewGuid().ToString(); 
    "Type"="TechnologyInteraction"; 
    "Name"= $row.Jobstream; 
    "Documentation"="$($row.JobDescription) `r`nServer: $($row.Server)  `r`nnuser: $($row.user) " ;  
  }
}

$output | Export-Csv $path\D.csv -NoTypeInformation -Encoding UTF8

列表格式的输出如下所示:

ID            : 85bf5f43-03fe-47af-9b04-84de8fe59f1c
Type          : TechnologyInteraction
Name          : PAXCWEBX965H
Documentation : Begin Of JobStream 
                Server:   
                nuser:  

ID            : 70e17ea9-2219-4836-b17f-9c9013a7ed6d
Type          : TechnologyInteraction
Name          : PAXCWEBX965H
Documentation : Lancement Sauv. RMAN 
                Server: PRAXCWBXLBDD01  
                nuser: svc_bddo_user 

【讨论】:

  • 非常感谢。对于第二部分,您是否认为,使用 $data,我可以创建一个哈希表“A”,其中包含调度列的值和生成的 ID 作为键,以生成 TechnologyEvent 类型
猜你喜欢
  • 1970-01-01
  • 2021-10-02
  • 2019-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-25
相关资源
最近更新 更多