所以这里有一个文件
APPLE: toronto, 2018, garden, tasty, 5
apple is a tasty fruit
>>>end
Orange: japan, 32, home, sour, 1
orange is a sour fruit
>>>end
graEes: america, 24, organic, sweet, 4
grapes is a sweet fruit
>>>end
这是一个也有新行字符的文件。 我想用这个文件创建一个字典。是这样的
函数是def f(file\u to:(TextIO))->;Dict[str,List[tuple]]
file\u to是输入的文件名,它将返回字典,如
{'apple': ['apple is a tasty fruit', 2018, 'garden', 'tasty', 5], orange:['orange is a sour fruit', 32,'home', 'sour',1] then grapes......}
每一个水果都是关键,它们的描述是格式化的。每种水果的结尾都是
我试过了
with open (file_to, "r") as myfile:
data= myfile.readlines()
return data
它返回一个带有/n的列表中的文件字符串,我想我可以使用strip()删除它,并将“:”前面的元素作为键。你知道吗
对于这个简单的示例,下面给出了所需的结果。(尽管你可能把葡萄拼错了)。你知道吗
相关问题 更多 >
编程相关推荐