How to add key & value to the dictionary from the text file?

Hi ,

I need to add Number column value as (key) & Account description column values as (value) into the dictionary from the text file. Kindly refer input text file.

Input1.txt (932 Bytes)

Can anyone guide me please, How to add into the dictionary?

Thanks,

Hi @jamunatj

Have a look on the thread

Hope it will helps

Regards
Gokul

Thanks for reply @Gokul001

I will check this but my input format is differ when compare to this file.

Thanks

Hey,

Can you try to convert the txt file to csv then add the key/value pairs from the csv row by row? Where the key would be YourDictionary(row(“NUMBER”).ToString.Trim) and the value row(“ACCOUNT DESCRIPTION”)

@jamunatj , you ca use GenerateDatatable activity to create the datatable with the input of type string and then you can add it to the dictionary with the appropriate columns as a key, value pair.

@jamunatj

Feel free to decompose the oneliner to part steps

File.ReadAllLines(strFilePath)
.Skip(1)
.Where(Function (x) Not String.IsNullorEmpty(x.Trim))
.Select(Function (x) Regex.Split(x.Trim,"\s{2,}"))
.toDictionary(Function (x) x.First(),Function (x) x.Last())
3 Likes

Thanks for the reply. Could you please provide any sample xaml file. It will be helpful to move further.

Thanks,

Thanks for the reply, Could you please provide any Sample xaml file .

Thanks,

As you asked for XAML Implementation help find some starter for the decomposed version here:

import: System.Text.RegularExpressions to the Namespaces (imports panel, next to variable panel)

strFilePath | String - FullFilePath to the text
dictLK | Dictionary(Of String, String), Default Value: new DIctionary(Of String, String)
arrSplit = Regex.Split(item.Trim,"\s{2,}")
dictLK(arrSplit.First) = arrSplit.last()
1 Like

Thanks for the reply @ppr

I have tried the code as you mentioned in the screenshot. I got error while assigning arrSplit even through imported system.Text.RegularExpressions . Could you please guide me to move on from this.
AddToDictionary.xaml (7.0 KB)

A dictionary could only hold one of those rows. You can’t put multiple rows in a dictionary. Why are you trying to convert to dictionary instead of just using the datatable?

we do miss the import in your xaml
and when adding it we got:
grafik
strict option binding issue

which is caused by:

and was mentioned by


in the above screenshot

Thanks the response, I have imported system.Text.RegularExpressions from package , still getting “Regex is not declared” error. Kindly suggest me to move further.

Thanks,

You Can Manually Write it as

System.Text.RegularExpressions.Regex.Split(YourStringVariable,RegexQuery)

which is done like:
RegexImport

so we can avoid the full qualified name System.Text.RegularExpressions and work directly with Regex.Split

please remove it again from the package manager as you can avoid side effects when not referencing unneeded packages