我是 PowerShell 新手,需要根据列值将 CSV 文件拆分为多个文件。
我的源文件是这样的
2 3 4 5 | 123 Jo 123456 123 Joe 789012 124 Tom 896578 124 Tom 403796 |
我阅读了这个帖子,它帮助我进行了分组,但我对如何根据 ID 列将其拆分为多个文件缺乏了解。这可能吗?
您编写了 csv,所以我假设您的文件的纯文本版本如下所示:
ID,姓名,TNumber
123,乔,123456
123,乔,789012
124,汤姆,896578
124,汤姆,403796
我会这样做:
#
2 3 4 5 6 7 8 9 10 11 12 13 14 | foreach($user in $users) { $id = $user.ID; $name = $user.Name; $tnumber = $user.TNumber; out-file -filepath id.csv -inputobject $id -append; out-file -filepath name.csv -inputobject $name -append; out-file -filepath tnumber.csv -inputobject $tnumber -append; } |
#
不是说如果你没有csv并且它是一个制表符分隔的文件,你可以在第一行添加以下属性:
-分隔符"`t"
希望这会有所帮助。
434511
这应该可以解决问题:
2 3 4 5 6 7 8 | foreach( $line in $fileContent ) { $lineToken = ($line -replace '\\s+', ' ').Trim() -split ' ' if( $lineToken[0] -match '^[0-9]+$' ) { $line | Out-File -FilePath ($lineToken[0] + '.csv') -Append } } |
434511
您可以找到唯一的 ID 列表,然后使用
2 3 4 5 | $IDs = $csv.ID | Select-Object -Unique foreach ($ID in $IDs) { $csv | Where-Object {$_.ID -eq $ID} | Export-CSV"C:\\example\\path\\$ID.csv" } |
使用
2 3 4 5 6 | $IDs = $csv.ID | Select-Object -Unique foreach ($ID in $IDs) { $newfile,$csv = $csv.where({$_.ID -eq $ID},'Split') $newfile | Export-CSV"C:\\example\\path\\$ID.csv" } |
这假定您显示的源文件是逗号分隔的 csv 文件的格式化导入。否则使用
434511
也许我把它复杂化了,但以防万一我假设 Name 列包含一个中间名。像这样的东西:
2 3 4 5 6 7 | 123 Jo 123456 123 Joe 789012 124 Tom 896578 124 Tom 403796 125 Jan W. 500300 125 Janny 700200 |
我的问题解决方法如下:
2 3 4 5 6 7 | $cap = $csv[0] -split '\\s+' # caption of CSV # replace spaces separating columns, group objects by ID ($csv[1..$csv.Length] -replace '(\\d+)\\s+(.*)\\s+(\\d+)', '$1,$2,$3' | ConvertFrom-Csv -Delimiter ',' -Header $cap | Group-Object ID).ForEach{ $_.Group | Export-Csv"$($_.Name).csv" # write result } |
434511
如果您的文件有制表符分隔符,您可以这样做:
2 3 4 5 6 | Import-Csv"$CurDir\\test.csv" -Delimiter"`t" | Group ID | %{ $ID="{0}.csv" -f $_.Name $_.Group | export-csv"$CurDir\\$ID" -NoType } |
434511
对不起,我误解了这个问题。重力,谢谢你的澄清。我认为其他一些答案可能有效,但如果他们不这样做,你可以试试这个。这可能是我会做的。请注意,我假设您有一个制表符分隔的文件,因此是 -delimiter"
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # Loop through users.csv foreach($user in $users) { # Put each field in a separate variable. $id = $user.ID; $name = $user.Name; $tnumber = $user.TNumber; # Write variables to host just to be sure the file is being read properly. This code can be commented out or removed after you are sure the file is being read. write-host $id; write-host $name; write-host $tnumber; write-host""; # If no file exists for a user with the ID contained in $id on this iteration, create it. if(!(Test-Path"$id.csv")) { out-file -filepath"$id.csv" -inputobject"ID`tName`tTNumber" -append; } # Append record for the user with the ID contained in $id on this iteration to $id.csv out-file -filepath"$id.csv" -inputobject"$id`t$name`t$tnumber" -append; } pause |
434511
Group-Object 按任何列值,这里我们使用 ID
2 3 | $groups |
输出
2 3 4 | ----- ---- ----- 2 123 {@{ID=123; Name=Jo; Tnumber=123456}, @{ID=123; Name=Joe;Tnumber=324233}} 2 124 {@{ID=124; Name=Tom; Tnumber=5645645}, @{ID=124; Name=Tom; Tnumber=23423}} |
最后把这个喂给for循环
434511
我来这里是为了寻找一个相当简单过程的快速答案,但大多数答案似乎对分组或行操作有点复杂。
下面的效果很好,我觉得更容易理解:
2 3 4 5 | foreach ($user in $users) { $user | Export-Csv -Path"C:\\example\\path\\$($user.ID).csv" -Append -NoTypeInformation } |
对于我的特殊情况,我们不希望输出文件中的特定列,所以我使用了 Select。在您的情况下,这看起来像:
2 3 4 5 | foreach ($user in $users) { $user | Select Name, TNumber | Export-Csv -Path"C:\\example\\path\\$($user.ID).csv" -Delimiter"`t" -Append -NoTypeInformation } |
434511
这是一个很老的问题。偶然发现了一个类似的场景,我必须根据文件中特定列的值从单个 csv 文件创建多个 CSV 文件。
我这样创建它是因为对我来说,我必须拆分的列名不是固定的
Group-Object 按任何列值,这里我们使用 ID
2 3 | $groups |
输出
2 3 4 | ----- ---- ----- 2 123 {@{ID=123; Name=Jo; Tnumber=123456}, @{ID=123; Name=Joe;Tnumber=324233}} 2 124 {@{ID=124; Name=Tom; Tnumber=5645645}, @{ID=124; Name=Tom; Tnumber=23423}} |
最后把这个喂给for循环
如果您的文件有制表符分隔符,您可以这样做:
2 3 4 5 6 | Import-Csv"$CurDir\\test.csv" -Delimiter"`t" | Group ID | %{ $ID="{0}.csv" -f $_.Name $_.Group | export-csv"$CurDir\\$ID" -NoType } |
这是一个很老的问题。偶然发现了一个类似的场景,我必须根据文件中特定列的值从单个 csv 文件创建多个 CSV 文件。
我这样创建它是因为对我来说,我必须拆分的列名不是固定的
我来这里是为了寻找一个相当简单过程的快速答案,但大多数答案似乎对分组或行操作有点复杂。
下面的效果很好,我觉得更容易理解:
2 3 4 5 | foreach ($user in $users) { $user | Export-Csv -Path"C:\\example\\path\\$($user.ID).csv" -Append -NoTypeInformation } |
对于我的特殊情况,我们不希望输出文件中的特定列,所以我使用了 Select。在您的情况下,这看起来像:
2 3 4 5 | foreach ($user in $users) { $user | Select Name, TNumber | Export-Csv -Path"C:\\example\\path\\$($user.ID).csv" -Delimiter"`t" -Append -NoTypeInformation } |
对不起,我误解了这个问题。重力,谢谢你的澄清。我认为其他一些答案可能有效,但如果他们不这样做,你可以试试这个。这可能是我会做的。请注意,我假设您有一个制表符分隔的文件,因此是 -delimiter"
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # Loop through users.csv foreach($user in $users) { # Put each field in a separate variable. $id = $user.ID; $name = $user.Name; $tnumber = $user.TNumber; # Write variables to host just to be sure the file is being read properly. This code can be commented out or removed after you are sure the file is being read. write-host $id; write-host $name; write-host $tnumber; write-host""; # If no file exists for a user with the ID contained in $id on this iteration, create it. if(!(Test-Path"$id.csv")) { out-file -filepath"$id.csv" -inputobject"ID`tName`tTNumber" -append; } # Append record for the user with the ID contained in $id on this iteration to $id.csv out-file -filepath"$id.csv" -inputobject"$id`t$name`t$tnumber" -append; } pause |
也许我把它复杂化了,但以防万一我假设 Name 列包含一个中间名。像这样的东西:
2 3 4 5 6 7 | 123 Jo 123456 123 Joe 789012 124 Tom 896578 124 Tom 403796 125 Jan W. 500300 125 Janny 700200 |
我的问题解决方法如下:
2 3 4 5 6 7 | $cap = $csv[0] -split '\\s+' # caption of CSV # replace spaces separating columns, group objects by ID ($csv[1..$csv.Length] -replace '(\\d+)\\s+(.*)\\s+(\\d+)', '$1,$2,$3' | ConvertFrom-Csv -Delimiter ',' -Header $cap | Group-Object ID).ForEach{ $_.Group | Export-Csv"$($_.Name).csv" # write result } |
您可以找到唯一的 ID 列表,然后使用
2 3 4 5 | $IDs = $csv.ID | Select-Object -Unique foreach ($ID in $IDs) { $csv | Where-Object {$_.ID -eq $ID} | Export-CSV"C:\\example\\path\\$ID.csv" } |
使用
2 3 4 5 6 | $IDs = $csv.ID | Select-Object -Unique foreach ($ID in $IDs) { $newfile,$csv = $csv.where({$_.ID -eq $ID},'Split') $newfile | Export-CSV"C:\\example\\path\\$ID.csv" } |
这假定您显示的源文件是逗号分隔的 csv 文件的格式化导入。否则使用
这应该可以解决问题:
2 3 4 5 6 7 8 | foreach( $line in $fileContent ) { $lineToken = ($line -replace '\\s+', ' ').Trim() -split ' ' if( $lineToken[0] -match '^[0-9]+$' ) { $line | Out-File -FilePath ($lineToken[0] + '.csv') -Append } } |
您编写了 csv,所以我假设您的文件的纯文本版本如下所示:
ID,姓名,TNumber
123,乔,123456
123,乔,789012
124,汤姆,896578
124,汤姆,403796
我会这样做:
#
2 3 4 5 6 7 8 9 10 11 12 13 14 | foreach($user in $users) { $id = $user.ID; $name = $user.Name; $tnumber = $user.TNumber; out-file -filepath id.csv -inputobject $id -append; out-file -filepath name.csv -inputobject $name -append; out-file -filepath tnumber.csv -inputobject $tnumber -append; } |
#
不是说如果你没有csv并且它是一个制表符分隔的文件,你可以在第一行添加以下属性:
-分隔符"`t"
希望这会有所帮助。
我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看rubyzip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d
我试图在一个项目中使用rake,如果我把所有东西都放到Rakefile中,它会很大并且很难读取/找到东西,所以我试着将每个命名空间放在lib/rake中它自己的文件中,我添加了这个到我的rake文件的顶部:Dir['#{File.dirname(__FILE__)}/lib/rake/*.rake'].map{|f|requiref}它加载文件没问题,但没有任务。我现在只有一个.rake文件作为测试,名为“servers.rake”,它看起来像这样:namespace:serverdotask:testdoputs"test"endend所以当我运行rakeserver:testid时
我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,
Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题
对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl
我有多个ActiveRecord子类Item的实例数组,我需要根据最早的事件循环打印。在这种情况下,我需要打印付款和维护日期,如下所示:ItemAmaintenancerequiredin5daysItemBpaymentrequiredin6daysItemApaymentrequiredin7daysItemBmaintenancerequiredin8days我目前有两个查询,用于查找maintenance和payment项目(非排他性查询),并输出如下内容:paymentrequiredin...maintenancerequiredin...有什么方法可以改善上述(丑陋的)代
我正在寻找执行以下操作的正确语法(在Perl、Shell或Ruby中):#variabletoaccessthedatalinesappendedasafileEND_OF_SCRIPT_MARKERrawdatastartshereanditcontinues. 最佳答案 Perl用__DATA__做这个:#!/usr/bin/perlusestrict;usewarnings;while(){print;}__DATA__Texttoprintgoeshere 关于ruby-如何将脚
我试图获取一个长度在1到10之间的字符串,并输出将字符串分解为大小为1、2或3的连续子字符串的所有可能方式。例如:输入:123456将整数分割成单个字符,然后继续查找组合。该代码将返回以下所有数组。[1,2,3,4,5,6][12,3,4,5,6][1,23,4,5,6][1,2,34,5,6][1,2,3,45,6][1,2,3,4,56][12,34,5,6][12,3,45,6][12,3,4,56][1,23,45,6][1,2,34,56][1,23,4,56][12,34,56][123,4,5,6][1,234,5,6][1,2,345,6][1,2,3,456][123
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何