【发布时间】:2018-10-02 08:45:55
【问题描述】:
我创建了这个函数,它获取文件的内容并将其翻译成字典。我有正确的格式,但我似乎无法弄清楚如何将列表内容变成一个元组。
这是我的功能,它没有元组部分:
def read(file:open) -> dict:
file_lines = file.read().splitlines()
result_dict = dict()
for string in file_lines:
splitted = string.replace(':', ';')
new_splitted = splitted.split(';')
client_name = new_splitted[0]
new_splitted = new_splitted[1:]
result_dict[client_name] = new_splitted
return result_dict
例如,我怎样才能使它成为一个元组?我尝试使用 tuple()、((x,)),但似乎没有任何效果。谢谢!
【问题讨论】:
-
tuple(x.split(','))
标签: python python-3.x csv dictionary