Datatable column to header?

Hey there,

I’ve managed to scrape a datatable I need from a website. I now have one column (A) which holds the field names and a second column (B) which has the entries for those fields.

What I would like to do is make it so that Column A is a header and then Column B is a variable (as it will change with each web page scraped).

Eg, It is currently like this:

Col1 Col 2
Name: Tim
Age: 45
Location: America

I want to make it so that the variable ‘name’ will be Tim, variable ‘Age’ will be ‘45’ and variable ‘Location’ will be America, for example. I think these would have to be strings also.

Could anyone help me with this please? Apologies, I’m new! Thank you.

Best way to do this would be using a Dictionary. You would have a variable dict of type System.Collections.Generic.Dictionary<String,String>

assign dict = new Dictionary(of String, String)
then for each row of the data table, use an assign activity with the below info:

for each row in myDataTable
  dict(row("Col1").toString) = row("Col2")

Now when you want to access someone’s name, you’d input dict("Name"), which returns Tim. Make sense?

Thanks for your reply, I think it makes sense in principle, however, I’m quite new to this so it has errored and I’m not quite there yet aha.

So my datatable (pDT3) is now in a for each loop and I added in the code to an assign as you mentioned (my dict variable is pDictionary).

I’m getting an error saying that row is not declared, any idea why this might be? It’s probably obvious to the more experienced than myself ahaha.

Is it a case that I have to create a variable for each field and then change ‘row’ in that code to the variable per line?

Thanks again.

on the variable pnael where you had defined the dictionary variable of datatype: Dictionary(Of String, String) use the default value field and enter:

new Dictionary(Of String, String)

as an lternate you can do (without for each row activity):

Assign activity
Left side: YourDictionaryVar | datatype: Dictionary(Of String, String)
Right side:

YourDataTableVar.AsEnumerable.ToDictionary(Function (x) x("Col1").toString.Trim,Function (x) x("Col2").toString.Trim)

I’ve done all this and (surprisingly) it didn’t error aha. How do I then view the output variables? If I could add them to the log too in order to see the output, that would be great.

go for this training:
Understanding the 6 Debugging Panels of UiPath in the easiest way possible! - News / Tutorials - UiPath Community Forum

1 Like

You would reference the dictionary and in the parameters, put the item you want to see. So either dict("Name") or dict("Age"), etc.

Just managed to do it! Thank you for your help, and also you @ppr!

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