导出 csv 只吐出长度 [英] Export csv spits out length only

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

问题描述

我只能在导出到csv时得到​​长度,应该如何正确完成.

$redo = 导入-CSV c:\temp\testimport.txt |组对象电子邮件 |foreach { "{0} ,{1}" -f $_.Name, (($_.Group | foreach { $_.group }) -join ', ')}$重做 |导出-CSV c:\temp\test.csv -NoTypeInformation

#<块引用>

长度"46"59"110"47"149"38"69"32"62"29"49"31""27" "48" "55" "42"

解决方案

Export-Csv 需要一个具有属性的对象(或对象列表),而您的命令管道产生一个字符串数组.如果您将此数组输入到 Export-Csv 中,则 cmdlet 会获取每个给定项的属性(对于字符串而言仅为 Length)并将这些属性写入输出文件.

您需要构建具有所需属性的对象列表,例如:

Import-CSV c:\temp\testimport.txt `|组对象电子邮件`|select @{n="Name";e={$_.Name}},@{n="Group";e={($_.Group | %{$_.group}) -join ', '}}`|导出-CSV c:\temp\test.csv -NoTypeInformation

I can only get the length when exporting this to csv, how should it be done properly.

$redo = Import-CSV c:\temp\testimport.txt | Group-Object email |
foreach {  "{0} ,{1}" -f $_.Name, (($_.Group | foreach { $_.group }) -join ', ')

}

$redo | Export-CSV c:\temp\test.csv -NoTypeInformation

#

"Length" "46" "59" "110" "47" "149" "38" "69" "32" "62" "29" "49" "31" "27" "48" "55" "42"

解决方案

Export-Csv expects an object (or a list of objects) with properties, whereas your command pipeline produces an array of strings. If you feed this array into Export-Csv the cmdlet takes the properties of each given item (which is only Length for strings) and writes those properties to the output file.

You need to build a list of objects with the desired properties instead, e.g.:

Import-CSV c:\temp\testimport.txt `
  | Group-Object email `
  | select @{n="Name";e={$_.Name}},@{n="Group";e={($_.Group | %{$_.group}) -join ', '}} `
  | Export-CSV c:\temp\test.csv -NoTypeInformation

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

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