Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在字典中打开文本文件-Python_Python_File_Dictionary_Load_Tuples - Fatal编程技术网

在字典中打开文本文件-Python

在字典中打开文本文件-Python,python,file,dictionary,load,tuples,Python,File,Dictionary,Load,Tuples,文本文件读取 1 2 3 4, Eric Wont, 1/1/10, 1,2,10 5 6, John Smith, 2/2/10, 2/2/11 我想把它存储在一个字典中,数字作为键,名称和日期作为元组 {4: (Eric Wont, 1/1/10, 1,2,10)} 它看起来是这样的: val_dict = {} for line in open('a.txt'): vals = line.strip().split(', ', 3)

文本文件读取

1
2
3
4, Eric Wont, 1/1/10, 1,2,10
5
6, John Smith, 2/2/10, 2/2/11
我想把它存储在一个字典中,数字作为键,名称和日期作为元组

{4: (Eric Wont, 1/1/10, 1,2,10)}

它看起来是这样的:

val_dict = {}
for line in open('a.txt'):
    vals = line.strip().split(', ', 3)                                                                                                                                
    val_dict[vals[0]] = tuple(vals[1:])

但这看起来并不是什么大问题,更像是一个家庭作业:)

欢迎光临!当您在这里发布问题时,请确保包含一个简短的代码片段,向我们展示您迄今为止的尝试。帮助我们更准确地定位您的问题。