Powershell processes each element read from CSV and outputs it to CSV

Asked 2 years ago, Updated 2 years ago, 90 views

1111, test2, test3, test4
111, test2, test3, test4
11, test 2, test 3, test 4

I would like to read the CSV file like the one above and erase the half-width space as shown below.

1111, test2, test3, test4
111, test2, test3, test4
11, test 2, test 3, test 4

In order to trim the half-width space of each CSV element, I wrote the following code in Powershell, but the CSV output is $results length.

Is there a solution?

$clms=@("col1", "col2", "col3", "col4");

$products=Import-Csv'C:\test\input.csv'-Header $clms-Encoding Default
$results=@()

foreach($product in $products){
 $line=@()
    foreach($clmin$clms){
        $line+=$product.$clm.Trim()
    }
 $results+=$line
}

$results | Export-Csv-Encoding Default-NoTypeInformation 'C:\test\output.csv'

powershell

2022-09-30 16:47

3 Answers

I think you can change the data you read and save it.

$clms=@("col1", "col2", "col3", "col4");

$products=Import-Csv'input.csv'-Header $clms-Encoding Default
$results=@()

foreach($product in $products){
  foreach($clmin$clms){
    $product.$clm=$product.$clm.Trim()
  }
}

$products | Export-Csv-Encoding Default-NoTypeInformation 'output.csv'

input.csv

1111, test2, test3, test4
111, test2, test3, test4
11, test 2, test 3, test 4

output.csv

"col1", "col2", "col3", "col4"
"1111", "test2", "test3", "test4"
"111", "test2", "test3", "test4"
"11", "test2", "test3", "test4"


2022-09-30 16:47

This article seems to be related to $results length.
Export-Csv seems to output different data.
Export-CSV exports length but not name

Export-Csvexports a table of object properties and their values.Since your script is producing string objects, and the only property they have is length, that's what you got.

If you just want to save the list, use Out-File or Set-Content installed of Export-Csv.

Export-Csv exports a table of object properties and their values. Scripts generate string objects, and the only property they have is length, so you get it.

If you only want to save the list, use Out-File or Set-Content instead of Export-Csv.

Then $line+=$product.$clm.Trim() or $results+=$line processing separates each column of data in one row and divides it into one row of data.
When $results+=$line, you will need to use about_Join to concatenate with commas.

As a countermeasure, change the two lines as follows: ### comments are the changes.

$clms=@("col1", "col2", "col3", "col4");

$products=Import-Csv'C:\test\input.csv'-Header $clms-Encoding Default
$results=@()

foreach($product in $products){
 $line=@()
    foreach($clmin$clms){
        $line+=$product.$clm.Trim()
    }
 $results+=$line-join', '#### If you don't connect, everything will be stored in separate lines
}

$results | Out-File-Encoding Default 'C:\test\output.csv'###Out-File instead of Export-Csv


2022-09-30 16:47

I was wondering if it should be treated as a CSV.Can't I just treat it as a text file and delete the blanks before ,?

Get-Content C:\test\input.csv-Encoding Default|
  ForEach-Object {$_-replace',',',','} |
  Set-Content C:\test\output.csv-Encoding Default


2022-09-30 16:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.