合并CSV文件中指定部门号的行



我正试图将具有以'8'开始的部门号码的行组合为一个owneremail(test@mail.com)。

原始:

| Employee ID | OwnerEmail      |DepartmentNumber |Costs    
| --------    | --------        | --------        | --------
| fg345       | test@mail.com   |8894             |4654.45
| 78f54       | test2@mail.com  |3453             |4994.15
| hg764       | test3@mail.com  |8892             |6543.20

期望:

| Employee ID | OwnerEmail    |DepartmentNumber |Costs     | 
| --------    | --------      |--------         |--------  |
| fg345       | test@mail.com |8894             |11,197.65 |
| 78f54       | test2@mail.co |3453             |4994.15   |

到目前为止我所尝试的:

if($_.DepartmentNumber.StartsWith('89')-or $_.DepartmentNumber.StartsWith('9r') -or $_.DepartmentNumber.StartsWith('92') -or $_.DepartmentNumber.StartsWith('hemecor') -or $_.DepartmentNumber.StartsWith('HEMECOR') -or [string]::IsNullOrWhiteSpace($_.DepartmentNumber))   {Group-Object}

同时,我对其他类似的方法持开放态度。

Group-Object应该有帮助

# Import your csv data 
Import-Csv .grouping.csv | 
# Use group-object to separate into different groupings
Group-Object -Property { 
# use the property parameter and pass it information on how you would like to group
# here I've choosen to use the first character of the DepartmentNumber value by index (zero-based index)
$_.DepartmentNumber[0] 
} | 
# do something with each group using ForEach-Object
# Here I'm creating new objects for each group which contain the following properties
# basically creating a summary with the sum of the costs
ForEach-Object {
[PSCustomObject]@{
EmployeeIDs       = $_.Group.EmployeeID -join ', '
OwnerEmails       = $_.Group.OwnerEmail -join ', '
DepartmentNumbers = $_.Group.DepartmentNumber -join ', '
CostSum           = ($_.Group.Costs | Measure-Object -Sum).Sum
}
}

输出
EmployeeIDs  OwnerEmails                   DepartmentNumbers  CostSum
-----------  -----------                   -----------------  -------
78f54        test2@mail.com                3453               4994.15
fg345, hg764 test@mail.com, test3@mail.com 8894, 8892        11197.65

您可以创建类似于"如果value等于或大于8000,则添加到列表中,可能使用命令。append(variable)",例如:

value = 8001

列表=(8446、8654、8123、8999)

if value =>8000:list.append(值)

但是考虑到我的例子是在python语言中,所以append命令可能在其他语言中不起作用

最新更新