Exzlanttt
Exzlanttt

Reputation: 73

How do i get a specific value from a dictionary?

i have a function that recieves two files .txt, one with tasks and another one with transaltors. Returns a list of assigned tasks to the translators.

Anyway thats not the point, Im trying to check if a given value on the translators dictionary is equal to another element present in the tasks file which is a list, this is the function btw:

def scheduleTasks(translators, tasks, date, hour, periodAfter):

    """Assigns translation tasks to translators.

    Requires:
    translators is a dict with a structure as in the output of
    readingFromFiles.readTranslatorsFile concerning the update time; 
    tasks is a list with the structure as in the output of 
    readingFromFiles.readTasksFile concerning the period of
    periodAfter minutes immediately after the update time of translators;
    date is string in format DD:MM:YYYY with the update time;
    hour is string in format HH:MN: with the update hour;
    periodAfter is a int with the number of minutes of a period 
    of time elapsed.
    Ensures:
    a list of translation tasks assigned according to the conditions
    indicated in the general specification (omitted here for 
    the sake of readability).
    """

My problem is how do I get the value from the dictionary, I know d.values() but this returns all the values like (this is the value from one key, just as an example) :

[' (portuguese; french)', ' (english)', ' 3*', ' 0.803', ' 3000', ' 25000', ' 3084', ' 08:11:2016\\n']

, but i just want the (portuguese; french) bit, if a try to run something like

for translator in translators.values():
      print translator[0]

It returns '[' , Im not sure but I think this is due to how linux writes files, this is the function im using to read the file:

def readTranslatorsFile(file_name):
    """Reads a file with a list of translators into a collection.

    Requires:
    file_name is str with the name of a .txt file containing
    a list of translators organized as in the examples provided in
    the general specification (omitted here for the sake of readability).
    Ensures:
    dict where each item corresponds to a translator listed in
    file with name file_name, a key is the string with the name of a translator,
    and a value is the list with the other elements belonging to that
    translator, in the order provided in the lines of the file.
    """
    inFile = removeHeader(file_name)       

    translatorDict = {}
    for line in inFile:

        key = line.split(",")[INDEXTranslatorName]
        value = line.split(",")[1::]
        translatorDict[key] = str(value)
    return translatorDict

And this is the input file:

Company:
ReBaBel
Day:
07:11:2016
Time:
23:55
Translators:
Ana Tavares, (english), (portuguese), 1*, 0.501, 2000, 20000, 2304, 08:11:2016
Mikolás Janota, (czech), (english; portuguese), 3*, 1.780, 2000, 200000, 4235, 08:11:2016
Paula Guerreiro, (french), (portuguese), 2*, 0.900, 3500, 45000, 21689, 11:11:2016
Peter Wittenburg, (dutch; english), (dutch; english), 2*, 1.023, 2500, 20000, 7544, 08:11:2016
Rita Carvalho, (english), (portuguese), 1*, 0.633, 5000, 400000, 18023, 09:11:2016
Steven Neale, (portuguese; french), (english), 3*, 0.803, 3000, 25000, 3084, 08:11:2016

Upvotes: 1

Views: 87

Answers (1)

Jean-François Fabre
Jean-François Fabre

Reputation: 140307

The problem is that when you parse your file you create a list using str.split() which is good, but then you convert back this list as string, instead of preserving the list as your values. That is bad.

translatorDict[key] = str(value)

I would do that:

  • split the line
  • discard the headers (not enough fields: list index out of range)
  • store the dictionary values as the list of tokens, much more flexible

code:

def readTranslatorsFile(file_name):

    inFile = removeHeader(file_name)       

    translatorDict = {}
    for line in inFile:
        tokens = line.split(",")  # split once and for good
        if len(tokens)>1:
          key = tokens[INDEXTranslatorName]
          value = tokens[1:]  # all items but first
          translatorDict[key] = value
    return translatorDict

(or use the csv module to handle comma-separated files more properly (quoting, etc))

Note that if INDEXTranslatorName is not zero, your method fails, then you could write value = tokens[:INDEXTranslatorName]+tokens[INDEXTranslatorName+1:] or tokens.pop(INDEXTranslatorName)

Then:

for translator in translators.values():
      print(translator[0].strip().replace(")","").replace("(",""))

efficiently prints your language tuples.

Upvotes: 1

Related Questions