Read 2 column comma delimited text into dictionary
I need some help with loading a 2 column comma delimited text file into a dictionary. I can read the file, so no problem. i can iterate through the file one at a time and add the values to a dictionary. But is there a way to efficiently read a text file as a dictionary object, or a more efficient and quicker way to create a dictionary from a textfile?
Jeff
-
If you already have the file contents in a string variable you could use this code in an Assign activity (add a reference to the System.Linq assembly). The result is a Dictionary(Of String, String). No checking is done for lines containing only a single value, or unique key values.
completeFile.Split({Environment.NewLine}, StringSplitOptions.None).Select(Function(s) s.Split(","c)).ToDictionary(Function(tokens) tokens(0), Function(tokens) tokens(1))
Unreadable indeed, but what it basically does is loop over each line, split on a comma and add to the dictionary. Here's a more verbose piece of code:
For Each line In completeFile.Split({Environment.NewLine}, StringSplitOptions.None)
Dim tokens = line.Split(","c)
dict.Add(tokens(0), tokens(1))
NextIt is arguable wether the 1st method is better/more efficient than other methods, but you only need one activity to create and assign the dictionary, which is what I prefer myself.
0
Bitte melden Sie sich an, um einen Kommentar zu hinterlassen.
Kommentare
1 Kommentar