Txt to dictionary

Hello. I am starting to learn UiPath. I have a problem.
We have a text file of the form:
Position=“pos1” Cost=“1923”
Position=“pos2” Cost=“5363”
Position=“pos3” Cost=“8634”
Position=“pos4” Cost=“2435”
You need to write this data into a dictionary:
pos1, 1923
pos2, 5363
pos3, 8634
pos4, 2435
help me please.

Hello, Welcome to the Forum, I hope you will have a good time here.

For this I would use split string, to split this data and take proper one, and also you can use vbnewrow for each new row. and for each row add it to dictinary.

Cheers,
Radomir

@111691
welcome to the forum

we can do with following:
grafik

  • read in the text file

  • split the text by Linebreaks

  • with the help of Regex we can extract the key and values

grafik

grafik

  • the extracted information can be added to the dictionary

find starter help here:
txtToDict_demo1.xaml (7.1 KB)
data1.txt (114 Bytes)

As an alternate we can do it with the help of LINQ:

grafik

(From line In strData.Split({Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
Let k =Regex.Match(line,"(?<=Position="").*?(?="")").toString
Let v =Regex.Match(line,"(?<=Cost="").*?(?="")").toString
Select Tuple.Create(k,v)).ToDictionary(Of String, String)(Function (t) t.item1,Function (t) t.item2)
3 Likes

Thank you friend. Your answer is the best, you helped me a lot. I am trying to display the key and value through MsgBox, but errors appear.
Скриншот 13-01-2021 220502

@111691
maybe the typeArgument was not set for the for each activity. Set to following type:
grafik

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