是的,我知道是否违反了CSV的标准,但我需要将数据提供给不能处理csv标头中的引号的应用程序。但是,它需要数据行中的引号。
我的进程采用完整的多行csv文件,并为每行数据分成单个csv。这是为了帮助隔离导入时的数据问题,因为目标系统的错误处理很差。这个过程有效。然后我发现引号是一个问题,我更改了脚本以删除所有引号,然后被告知只需要删除标题行中的引号。
我对如何实现这一点感到茫然。我想写没有引号的标题并包含在数据行中。其他一些想法是用引号写入CSV,然后在第二步中删除第一行的引号。我考虑过编写没有标题的文件,并在每个创建的csv中添加一个包含标题的文件,但每次都可以改变列。 任何建议,将不胜感激。代码如下:
#This script reads the combined csv file and splits it into multiple csv files,
#one for each row is in the sheet. Place original in the input folder under the folder this script is
#located in. Make sure there is an output folder. Script will then place multiple files in output folder.
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
#Set Working directory
Set-Location c:\documents\powershell\;
#remove existing files
Remove-Item .\output\*.csv
# variable used to store the path of the source CSV file - calls function specifying default directory
$sourceCSV = Get-FileName "C:\documents\powershell" ;
#echo $sourceCSV;
# get max number of rows
$maxrows = Import-Csv $sourceCSV | Measure-Object | Select-Object -expand count;
#echo $maxrows;
# variable used to advance the number of the row from which the export starts
$startrow = 0 ;
# counter used in names of resulting CSV files
$counter = 1 ;
while ($startrow -lt $maxrows)
{
Import-CSV $sourceCSV | select-object -skip $startrow -first 1 | convertto-csv -NoType | % { $_ -replace '"', ""} | out-file ".\output\$($counter).csv" -fo -en ascii;
#Import-CSV $sourceCSV | select-object -skip $startrow -first 1 | Export-CSV ".\output\$($counter).csv" -NoClobber -NoType;
$startrow += 1 ;
$counter++ ;
}
输入后 - 解决方案是:
Function Get-FileName($initialDirectory)
{
[System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = $initialDirectory
$OpenFileDialog.filter = "CSV (*.csv)| *.csv"
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
#Set Working directory
Set-Location c:\documents\powershell\;
#remove existing files
Remove-Item .\output\*.csv
# variable used to store the path of the source CSV file - calls function specifying default directory
$sourceCSV = Get-FileName "C:\documents\powershell" ;
$maxrows = Import-Csv $sourceCSV | Measure-Object | Select-Object -expand count;
#csv does not count header row
$maxrows = $maxrows+1;
# variable used to advance the number of the row from which the export starts - set to 1 since we are including first row everytime
$startrow = 1 ;
$counter = 1 ;
$csv = Get-Content $sourceCSV;
while ($startrow -lt $maxrows)
{
# import of however many rows you want the resulting CSV to contain starting from the $startrow position and export of the imported content to a new file
($csv[0] -replace '"',''),$csv[$startrow] | Set-Content ".\output\$($counter).csv" -Force
$startrow += 1 ;
$counter++ ;
}