我正在使用 PowerShell 导入带有标题的制表符分隔文件。生成的文件在标题的第一行末尾有几个空字符串“”。 PowerShell 失败并出现错误:
"Cannot process argument because the value of argument "name" is invalid. Change the value of the "name" argument and run the operation again"
因为标题需要一个名称。
我想知道是否有人对如何操作文件以删除双引号或用“1”“2”“3”......“10”等枚举它们有任何想法。
理想情况下,我不想修改我的原始文件。我在想这样的事情
$fileContents = Get-Content -Path = $tsvFileName
$firstLine = $fileContents[0].ToString().Replace('`t""',"")
$fileContents[0] = $firstLine
Import-Csv $fileContents -Delimiter "`t"
但是 Import-Csv 期望 $fileContents 是一个路径。我可以让它使用内容作为来源吗?
最佳答案
您可以提供自己的标题并忽略 csv 的第一行,也可以像 Keith 所说的那样在末尾使用 convertfrom-csv。
ps> import-csv -Header a,b,c,d,e foo.csv
现在文件中的无效标题只是您可以跳过的一行。
-Oisin
关于带有空标题的 Powershell 导入 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3373200/