Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.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 - Fatal编程技术网

Python,如何使用文件中的行作为字典中的键,使用下一行作为值?

Python,如何使用文件中的行作为字典中的键,使用下一行作为值?,python,file,dictionary,Python,File,Dictionary,我有这样一个文件: Ben 0 1 5 2 0 1 0 1 Tim 3 2 1 5 4 0 0 1 {Ben: 0 1 5 2 0 1 0 1, Tim : 3 2 1 5 4 0 0 1} 我想制作一本如下所示的词典: Ben 0 1 5 2 0 1 0 1 Tim 3 2 1 5 4 0 0 1 {Ben: 0 1 5 2 0 1 0 1, Tim : 3 2 1 5 4 0 0 1} 所以我在想: for lin

我有这样一个文件:

   Ben
   0 1 5 2 0 1 0 1
   Tim
   3 2 1 5 4 0 0 1
    {Ben: 0 1 5 2 0 1 0 1, Tim : 3 2 1 5 4 0 0 1}
我想制作一本如下所示的词典:

   Ben
   0 1 5 2 0 1 0 1
   Tim
   3 2 1 5 4 0 0 1
    {Ben: 0 1 5 2 0 1 0 1, Tim : 3 2 1 5 4 0 0 1}
所以我在想:

   for line in file:
       dict[line] = line + 1
但是你不能遍历这样的文件,那么我该怎么做呢
这样做?

这可能是您想要的:

state = 0
d = {}
for line in file:
    if state == 0:
        key = line.strip()
        state = 1
    elif state == 1:
        d[key] = line.split()
        state = 0
dict_data = {}
with open('data.txt') as f:
    for key in f:
        dict_data[key.strip()] = next(f).split()

print dict_data
输出:

{'Tim':['3','2','1','5','4','0','0','1','Ben':['0','1','5','2','0','1','1']}

讨论 for循环假设每一行都是键,我们将读取循环体中的下一行 key.strip将“Tim\n”变为“Tim” f、 next读取并返回下一行-键行之后的行 f、 因此,将该行拆分为一个列表 dict_数据[key.strip]=。。。将执行如下操作:dict_data['Tim']=[…] 使现代化 感谢布尔克恩赫特的指点。我在nextf旁边换了f 更新2 如果要将列表转换为整数列表,而不是字符串,请执行以下操作:

        dict_data[key.strip()] = next(f).split()
这样做:

        dict_data[key.strip()] = [int(i) for i in next(f).split()]

这可能是您想要的:

dict_data = {}
with open('data.txt') as f:
    for key in f:
        dict_data[key.strip()] = next(f).split()

print dict_data
输出:

{'Tim':['3','2','1','5','4','0','0','1','Ben':['0','1','5','2','0','1','1']}

讨论 for循环假设每一行都是键,我们将读取循环体中的下一行 key.strip将“Tim\n”变为“Tim” f、 next读取并返回下一行-键行之后的行 f、 因此,将该行拆分为一个列表 dict_数据[key.strip]=。。。将执行如下操作:dict_data['Tim']=[…] 使现代化 感谢布尔克恩赫特的指点。我在nextf旁边换了f 更新2 如果要将列表转换为整数列表,而不是字符串,请执行以下操作:

        dict_data[key.strip()] = next(f).split()
这样做:

        dict_data[key.strip()] = [int(i) for i in next(f).split()]

我认为最简单的方法是首先用file.readlines加载完整文件,它加载整个文件并返回一个行列表。然后,您可以创建具有以下理解的词典:

lines = my_file.readlines()
my_dict = dict(lines[i:i+2] for i in range(0, len(lines), 2))
对于您的示例文件,这将给出我的目录内容:

{"Ben\n": "0 1 5 2 0 1 0 1\n", "Tim\n": "3 2 1 5 4 0 0 1\n"}
另一种方法是使用while循环,一次读取两行:

my_dict = {}
while True:
    name = file.readline().strip()
    if not name: # detect the end of the file, where readline returns ""
        break
    numbers = [int(n) for n in file.readline().split()]
    my_dict[name] = numbers
与早期版本中的理解相比,这种方法允许您轻松地对行进行一些处理,例如剥离换行符和将数字行拆分为实际int对象的列表

示例文件的结果是:

{"Ben": [0, 1, 5, 2, 0, 1, 0, 1], "Tim": [3, 2, 1, 5, 4, 0, 0, 1]}

我认为最简单的方法是首先用file.readlines加载完整文件,它加载整个文件并返回一个行列表。然后,您可以创建具有以下理解的词典:

lines = my_file.readlines()
my_dict = dict(lines[i:i+2] for i in range(0, len(lines), 2))
对于您的示例文件,这将给出我的目录内容:

{"Ben\n": "0 1 5 2 0 1 0 1\n", "Tim\n": "3 2 1 5 4 0 0 1\n"}
另一种方法是使用while循环,一次读取两行:

my_dict = {}
while True:
    name = file.readline().strip()
    if not name: # detect the end of the file, where readline returns ""
        break
    numbers = [int(n) for n in file.readline().split()]
    my_dict[name] = numbers
与早期版本中的理解相比,这种方法允许您轻松地对行进行一些处理,例如剥离换行符和将数字行拆分为实际int对象的列表

示例文件的结果是:

{"Ben": [0, 1, 5, 2, 0, 1, 0, 1], "Tim": [3, 2, 1, 5, 4, 0, 0, 1]}

位解释对我和OP来说都很难理解,我无法理解dict_data[key.strip]=f.next.split line.key.strip删除了空格,尤其是换行符。f、 next.split获取文件中的下一行,并将其拆分为一个成员列表。非常感谢,这正是我想要的-简单而高效。f.next调用非常隐蔽。它之所以有效,是因为文件是它自己的迭代器!值得注意的是,在Python3中,您需要使用nextf,因为迭代器的下一个方法被重命名为uu next_uu。位解释对我和OP来说都是grate,我无法理解dict_data[key.strip]=f.next.split line.key.strip删除空白,尤其是换行符。f、 next.split获取文件中的下一行,并将其拆分为一个成员列表。非常感谢,这正是我想要的-简单而高效。f.next调用非常隐蔽。它之所以有效,是因为文件是它自己的迭代器!值得注意的是,在Python3中,您需要使用nextf,因为迭代器的下一个方法被重命名为uu next_uu。