cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
virakones
Responsive Resident
Responsive Resident

How to split & parse CSV file with comma inside a value?

Hi,

 

Running into some challenges attempting to parse through a CSV file that is using the expression split( outputs( 'CSVData'), ',') in a Compose action.

 

In the example below, I have some values that contain a comma, so the expression will inadvertently split the value itself along the comma.

 

CSVData: Value1,Value2,"Value,3",Value4

 

The split will create an incorrect array:

 

[

"Value1",

"Value2",

"Value",

"3",

"Value4"

]

 

What are some ways to avoid the above? Ideally it should remain "Value,3" - I imagine using the double quotes (") in the CSV would be a starting point? Looking to keep it within just standard actions available in power automate.

1 ACCEPTED SOLUTION

Accepted Solutions
virakones
Responsive Resident
Responsive Resident

@PrasadAthalye

Following up to the solution here: Solved: Re: Setting up specific expression to remove comma... - Power Platform Community (microsoft....

As mentioned in the thread, the final "ReplacedText" is my CSV file entirely missing commas - which I still needed intact for later use in the flow. I only wanted to replace the extra commas in the values itself as they appear, and then keep my CSV largely intact.

However, I noticed that when the CSV was split into an array from the double quotes (") it managed to create a consistent pattern, specifically when I was flipping through the Apply to Each Stage of the solution. 

Apply to Each 1/10 - CSV Data (Remains intact)

> Apply to Each 2/10 - Value,1 (Comma should be replaced here)

 > Apply to Each 3/10 - CSV Data (Remains intact)

  > Apply to Each 2/10 - Value,2 (Comma should be replaced here)

etc.


This helped me find an addition to the solution using an incrementing integer in a Do Until loop using Mod expression in a condition to detect whether the increment is odd or even (if odd, do nothing, if even replace comma).

1. (CSVCount) Add Initialize Integer Variable after creating CSV array variable to use to count against: 

length( variables( 'CSVDataArray' ) )

2. (arrayCount) Add Initialize Integer Variable to Increment with later: starting value 1 

 

3. Put Apply to Each in "Do Until" loop: Do until arrayCount is greater than CSVCount

 

4. Inside Apply to Each, add a condition to check whether arrayCount is odd or even using Mod:
mod(variables('arrayCount'),2) is equal to 1 (if equal, its odd, if not equal, its even)

5. If yes, Append to ReplacedText as is (current item of Apply to Each - the intact CSV data)

6. If no, Replace the comma in the value, then append to ReplacedText
replace(variables('TextAfterSplit'), ',', '')

 

7. After condition, still inside Apply to Each, increment variable arrayCount by 1

 

8. Finally compose the ReplacedText 

Image Below:

addon.jpg

View solution in original post

3 REPLIES 3
virakones
Responsive Resident
Responsive Resident

@PrasadAthalye

Following up to the solution here: Solved: Re: Setting up specific expression to remove comma... - Power Platform Community (microsoft....

As mentioned in the thread, the final "ReplacedText" is my CSV file entirely missing commas - which I still needed intact for later use in the flow. I only wanted to replace the extra commas in the values itself as they appear, and then keep my CSV largely intact.

However, I noticed that when the CSV was split into an array from the double quotes (") it managed to create a consistent pattern, specifically when I was flipping through the Apply to Each Stage of the solution. 

Apply to Each 1/10 - CSV Data (Remains intact)

> Apply to Each 2/10 - Value,1 (Comma should be replaced here)

 > Apply to Each 3/10 - CSV Data (Remains intact)

  > Apply to Each 2/10 - Value,2 (Comma should be replaced here)

etc.


This helped me find an addition to the solution using an incrementing integer in a Do Until loop using Mod expression in a condition to detect whether the increment is odd or even (if odd, do nothing, if even replace comma).

1. (CSVCount) Add Initialize Integer Variable after creating CSV array variable to use to count against: 

length( variables( 'CSVDataArray' ) )

2. (arrayCount) Add Initialize Integer Variable to Increment with later: starting value 1 

 

3. Put Apply to Each in "Do Until" loop: Do until arrayCount is greater than CSVCount

 

4. Inside Apply to Each, add a condition to check whether arrayCount is odd or even using Mod:
mod(variables('arrayCount'),2) is equal to 1 (if equal, its odd, if not equal, its even)

5. If yes, Append to ReplacedText as is (current item of Apply to Each - the intact CSV data)

6. If no, Replace the comma in the value, then append to ReplacedText
replace(variables('TextAfterSplit'), ',', '')

 

7. After condition, still inside Apply to Each, increment variable arrayCount by 1

 

8. Finally compose the ReplacedText 

Image Below:

addon.jpg

Hi,

 

Thanks for your follow up post! I am glad you were able to find a solution for your issue. Hopefully this will prove useful for future communities users who are having similar issues.

 

Regards,

 

Alex

 

-------

 

Community Support Team _ Alex Rezac
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

takolota
Super User
Super User

Hello,

 

Here’s a flow that can change only the column separator commas to a different delimiter in similar CSV files with quotes around the comma-containing lines. It only uses a few actions to do this, even on large CSV files.
And you can make more files put quotes around the comma-containing records if you save them as a text file.

 

https://powerusers.microsoft.com/t5/Power-Automate-Cookbook/Change-CSV-File-Data-Delimiter/m-p/14429...


Once all the correct commas are replaced it’s pretty easy to parse the entire file in a single Select action.

Helpful resources

Announcements
Power Automate News & Announcements

Power Automate News & Announcements

Keep up to date with current events and community announcements in the Power Automate community.

Community Calls Conversations

Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Automate Community Blog

Power Automate Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (1,636)