def fileToDict(filename): """ >>> fileToDict('testDict.txt') == {'student_name': 'Robert', 'teacher_name':'Smith'} True """ string = fileToString(filename) result = {} for line in string.strip().split("\n"): item = line.split(":") result[item[0]] = randomly_pick(item[1].strip().split()) return result
def stringToDict(string): """ >>> stringToDict(fileToString('testDict.txt')) == {'student_name': 'Robert', 'teacher_name':'Smith'} True >>> stringToDict('name:mason\\nage:18\\n') {'age': '18', 'name': 'mason'} """ #- >>> stringToDict('name:firstname surname\\nbirthplace:city name\\n') #- {'birthplace': ['city', 'name'], 'name': ['firstname', 'surname']} #- """ result = {} for line in string.strip().split("\n"): item = line.split(":") result[item[0]] = randomly_pick(item[1].strip().split()) return result