Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Powershell:将文本文件转换为csv文件_Python_Powershell_Csv - Fatal编程技术网

Python Powershell:将文本文件转换为csv文件

Python Powershell:将文本文件转换为csv文件,python,powershell,csv,Python,Powershell,Csv,这是一个脚本,我从网上得到,我想把一个文件夹的文本文件转换为csv文件。 此代码仅允许我将所有txt文件放入一个csv文件中。 我想将多个txt文件转换为具有相同名称的多个csv文件。对于选择字符串,您需要引用模式,例如: $SearchFolder = "C:\Test\New folder\*.txt" $CSVFilePath = "C:\Test\txtfiles" # this doesn't work... $File = Select-String -Path $Sea

这是一个脚本,我从网上得到,我想把一个文件夹的文本文件转换为csv文件。 此代码仅允许我将所有txt文件放入一个csv文件中。
我想将多个txt文件转换为具有相同名称的多个csv文件。

对于选择字符串,您需要引用模式,例如:

$SearchFolder = "C:\Test\New folder\*.txt" 


$CSVFilePath = "C:\Test\txtfiles" # this doesn't work... 


$File = Select-String -Path $SearchFolder -Pattern new-print-server, default, dymo, zebra -NotMatch | select line, filename



$File | foreach-object {
$_.line = $_.line.replace("^",",")

}


$File | export-csv -path $CSVFilePath -NoTypeInformation


$csv = Get-Content -path $CSVFilePath



$csv | Out-File $CSVFilePath -Encoding utf8


Invoke-Item $CSVFilePath
看看这是否能让你更接近:

Select-String -Path $SearchFolder -Pattern 'new-print-server, default, dymo, zebra' -NotMatch | select line, filename

谢谢你的帮助。但它给了我两列行和文件名,所有数据都集中在一列(行)中。我不想看到行或文件名。。我想将txt转换为csv文件,不做任何更改。。我知道我发布的脚本有选择行、文件名。。。很抱歉出现混淆在这种情况下,请将最后一行更改为
$file |导出Csv…
$files = Get-ChildItem "C:\Test\New folder\*.txt"
foreach ($file in $files) {
    $notMatchedLines = $file | Select-String -Pattern 'new-print-server, default, dymo, zebra' -NotMatch | select line,filename
    $notMatchedLines | foreach {$_.line = $_.line.replace("^",",")}
    $notMatchedLines | Export-Csv ([io.path]::ChangeExtension($file.Fullname, '.csv')) -NoTypeInformation -Encoding UTF8
}