Python将文本文件转换为字典

use*_*807 0 python python-3.x

我正在写一个拼写检查功能,我有一个看起来像这样的文本文件

teh the
cta cat
dgo dog
dya day
frmo from
memeber member
Run Code Online (Sandbox Code Playgroud)

拼写错误在左边(这将是我的键),正确的拼写在右边(我的值).

def spell():
    corrections=open('autoCorrect.txt','r')
    dictCorrect={}
    for line in corrections:
        corrections[0]=[1]
        list(dictCorrect.items())
Run Code Online (Sandbox Code Playgroud)

我知道我希望我的功能做什么,但无法弄清楚如何执行它.

Inb*_*ose 5

用这个:

with open('dictionary.txt') as f:
    d = dict(line.strip().split(None, 1) for line in f)
Run Code Online (Sandbox Code Playgroud)

d 是字典.

免责声明: 这将适用于您上面说明的简单结构,对于更复杂的文件结构,您需要进行更复杂的解析.