导入/导出.csv文件 [英] Import/Export .csv file

查看:134
本文介绍了导入/导出.csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有带100个日期的testDates.csv

I have testDates.csv with 100 dates


date
1/10/17
6/10/18
9/10/42
...

我制作了一个脚本,可以根据以下日期创建示例密码:

I made a script that makes sample passwords based off these dates:

$file = Import-Csv -Path U:\Desktop\testDates.csv

foreach ($line in $file) {
    $fullDate = $line.date
    $year = Get-Date $fullDate -Format "yy"
    $currentDate = Get-Date $fullDate -Format "MM-dd"

    # Determining season
    if ($currentDate -ge (Get-Date 01-01) -and $currentDate -lt (Get-Date 03-01)) {
        $season = "Winter"
    } elseif ($currentDate -ge (Get-Date 03-01) -and $currentDate -le (Get-Date 05-31)) {
        $season = "Spring"
    } elseif ($currentDate -ge (Get-Date 06-01) -and $currentDate -le (Get-Date 08-31)) {
        $season = "Summer"
    } elseif ($currentDate -ge (Get-Date 09-01) -and $currentDate -le (Get-Date 11-30)) {
        $season = "Fall"
    } elseif ($currentDate -ge (Get-Date 12-01) -and $currentDate -le (Get-Date 12-31)) {
        $season = "Winter"
    } else {
        $season = "ClearyAnIntern"
    }

    # Creating password
    $SamplePassword = $season + $year
}

我想将创建的密码导出到新列中的原始.csv.因此,在日期"旁边有一列密码",以查看我的脚本是否正常工作.示例:

I want to export the created password back to the original .csv in a new column. So have a column "passwords" next to "dates" to see if my script works correctly. Example:


date    password
1/10/17 Winter17
6/10/18 Summer18
9/10/42 Fall42
...     ...

我知道它涉及到Export-Csv,但是我不知道如何将其特别用于每行.我一直在寻找大量不同的线程,并在MS帮助中心中找到如何使用不同的参数(例如-Append等),但是我所看到和尝试的一切都不完全是我正在处理的内容,并且从这些例子中推断,目前不在我的补救知识范围内.我开始相信这可能涉及嵌套的foreach循环.

I know it involves Export-Csv but I don't know how to pipe it especially for each line. I've been looking at tons of different threads and the MS help center for how to use the different parameters such as -Append and so forth but everything I've seen and tried isn't exactly what I'm dealing with and to infer from those examples is outside the scope of my remedial knowledge at the moment. I'm beginning to believe this may involve nested foreach loops.

推荐答案

Import-Csv C:\path\input.csv | ForEach-Object {
   $seasons = [Char[]]'wwsssmmmfffw' # seasons map
}{ # walk around CSV lines
   [PSCustomObject]@{
      Date = $_.date # first and second columns of future CSV
      Password = "$(switch ($seasons[([DateTime]$_.date).Month - 1]) {
         'w' {'Winter'} 's' {'Spring'} 'm' {'Summer'} 'f' {'Fall'}
      })$(Get-Date $_.date -Format yy)"
   }
} | Export-Csv C:\path\out.csv # export result

这篇关于导入/导出.csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆