コード例 #1
0
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
コード例 #2
0
ファイル: myCode.py プロジェクト: codezoners-2/submissions
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