Python,排序fi

2024-09-29 19:19:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个函数,对文件中的数据进行排序,并将其放入字典中。我做了以下工作:

def fileread(filename, filetype, readlist):
returnslist = {}
with open(filename + "." + filetype, "r") as f:
    for line in f:
        for entry in readlist:
            if(line[:(entry.len() + 2)] == entry):
                returnslist[entry] = line[(entry.len() + 2):]

目前我使用的笔记本电脑不能使用Python,所以我正在开发一个不能使用文件的在线IDE。这样行吗?我想让这个文件像配置文件一样工作。例如:

Int1: 2
Int2: 12
Int3: 32
Int4: 65
Str1: Lol
Bool1: True

我的代码应该成为字典:

{"Int1": "2", "Int2": "12", "Int3": "32", "Int4": "65", "Str1": "Lol", "Bool1": "True"}

有没有更干净的方法?我想不出什么了。你能看到这里面有什么错误吗?你知道吗


Tags: 文件inforlen字典linefilenamefiletype

热门问题