How to read a CSV file row by row and if condition is met then add to a new row?

Hey,

I’m trying

  • to read from a csv file through read csv activity.
  • read the rows one by one.
  • there are columns which acts as condition and if they are met, add the data to a new row.
  • continue reading until all the given conditions are met.
  • write it in one csv file.

I’ve tried many other way to achieve the above case but I’m unable to include all the conditions.
I’d be grateful!
Thanks in advance

Hi
Welcome to uipath community
—hope these steps would help you resolve this
—use READ CSV FIle Activity and pass the file path of csv as input
—then use FOR EACH activity and pass the able variable dt as input like this dt.Rows
—inside the loop use IF condition and set the condition we want
—if it passes it will go to THEN part where we can use ADD DATAROW activity and mention the datatable dt as input for datatable property and in Datarow property mention as dt.NewRow

This will add the new rows

Cheers @ray_sha

Heyy @Palaniyappan
Thanks for the reply!

  • I’ve tried doing the same, but it write only one among all the given conditions.
  • gives errors when I use the For each row as I’ll be adding new rows to the same datatable!
  • ADD DATA ROW, I did use dt.NewRow, still I couldn’t solve it.

I’ll follow your steps and try again!
It’d be helpful if you help me with the details.

Thanks in advance!

1 Like

Hey @Palaniyappan

I’ll Change that and let you know!

Thank you!

1 Like

Hello Ray,

It is not a good idea to modify a collection while you are looping through it. Instead, what you should do is loop through your CSV as Palaniyappan suggested, and, if the condition is met, add a new row to a different DataTable. This will also ensure that you will at some point stop as you could potentially run into an infinite loop if you keep adding new rows that meet the condition.

1 Like

There’s no reason to use For Each here when there’s already a For Each Row activity that defaults to System.Data.Row.

aaah …i m sorry for that
both FOR EACH ROW or FOR EACH wont work as we wont be able to add new datarow to a datatble which is actaully under iteration
so we can use while loop like this
counter < dt.Rows.Count
where counter is a variable of type int32 with default value as 0 defined in the variable panel
–inside the while loop use a IF condition like this
dt.Rows(Counter)(“yourcolumnname”).ToString.Contains(“your keyword”)
if true it will go to THEN part where we can use ADD DATAROW activity with datatable as dt and DataRow as dt.NewRow

Cheers @dmccammond @ray_sha

This still is not suggestible. All you did is try to circumvent the error that is rightly generated for attempting to add to a collection while looping through it. It is not a good idea to do this, instead a new DataTable should be populated unless the user is absolutely certain that they want the added rows to be checked as well.

Hey @dmccammond @Palaniyappan
Thank you for the reply’s.
I apologize for replying so late!

Yes! for each row and for each , both didn’t work and gave an error.

So you guys suggest that I use While loop and assign the same to a new datatable?
Please feel free to correct me if I’m wrong.

Thanks in advance!

yah pls try with that step

and let know for any queries or clarification
Cheers @ray_sha

1 Like

Hey @Palaniyappan
I followed the same steps, but got a error message.

Error message

Source: Add data row
Message: Object reference not set to an instance of an object.
Exception Type: System.NullReferenceException

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is:
System.NullReferenceException: Object reference not set to an instance of an object.
at UiPath.Core.Activities.AddDataRow.Execute(CodeActivityContext context)
at System.Activities.CodeActivity.InternalExecute(ActivityInstance instance, ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.ActivityInstance.Execute(ActivityExecutor executor, BookmarkManager bookmarkManager)
at System.Activities.Runtime.ActivityExecutor.ExecuteActivityWorkItem.ExecuteBody(ActivityExecutor executor, BookmarkManager bookmarkManager, Location resultLocation)

As @dmccammond had warned, if i use the same datatable then it enters a infinite loop!
and if a new datatable is given then the above error message is displayed.

Can you please guide me with the add data row activity.
Thanks in advanvce!

Is there any other approach to this problem?? Or is this not possible using UiPath?

can i have a sample csv file and key word to be searched for in a column
so that i can simulate the process and get back
Cheers @ray_sha

1 Like

The input file consists of

  • 5 columns named Id, name, hours, additional hours(10%), additional hours (20%)

  • if the values in the column - hours is other than 0, then in the output file under a column- type --> enter - regular and scale must be --> 100

  • if the value in the column – additional hours (10%) is other then 0, then in the output file under column- type --> enter - extra and scale must be --> 10

  • if the value in the column – additional hours (20%) is other then 0, then in the output file under column- type --> enter - extra and scale must be --> 20

The output file has 5 columns named

  • id
  • name
  • hours
  • type
  • scale

I’ll be attaching the images of both the csv files

Hey,
Could you find any way to add in new row’s?

Is there any approach where we could convert each column into a table and match the values in the other columns and add in new row to that particular table??

Thanks in Advance!

The Input CSV file for the above is attached below:
input.zip (195 Bytes)

Thanks in Advance!

Hey,
Did you find a way to achieve it?

Or will this not be possible using UiPath?

If anyone has any clue regarding the same, Please let me know.
Thanks in Advance!

This should calm your curiosity.

Thanks for Trying!

Here’s the solution.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.