简体   繁体   English

使用文件中的数据创建字典值列表

[英]Creating a List of Dictionary Values with data from a file

I have a method that inputs a path/directory to a folder and a filename. 我有一种输入文件夹的路径/目录和文件名的方法。 I need to retrieve the data from the specified file name in the folder and create a LIST of DICTIONARIES . 我需要从文件夹中的指定文件名检索数据,并创建一个LIST of DICTIONARIES

PART of an example file is below: 示例文件的一部分如下:

VLSEGEWQLVLHVWAKVEADVAGHGQDILIRLFKSHPETLEKFDRFKHLKTEAEMKASEDLKK

1 137 0 8 7.887

10 127 0 8 7.388

10 130 0 8 5.087

11 131 0 8 5.400

10 134 0 8 4.770

The FIRST LINE of each file should be ignored. 每个文件的第一行都应忽略。 Then, the first two numbers in each row will be the values assigned to key value 'pairs', the 0 and the 8 are ignored, and the last number is the value assigned to 'distance' this is done to each row in the file, with each row being a new dict. 然后,每行的前两个数字将是分配给键值“ pairs”的值, 08将被忽略,最后一个数字是分配给“距离”的值,这是对文件中的每一行进行的操作,每一行都是新的字典。

So the output should look like 所以输出应该像

output = [
   {"pairs": (1,137), "distance": 7.887},

   {"pairs": (10,127), "distance": 7.388},

   {"pairs": (10,130), "distance": 5.087},

   {"pairs": (11,131), "distance": 5.400},

   {"pairs": (10,134), "distance": 4.770},

]

I am not sure how to approach this problem, whether its how to read the file and each row of the file. 我不确定该如何解决该问题,无论它如何读取文件以及文件的每一行。 What I have so far is pretty basic. 到目前为止,我所掌握的基本知识。 I haven't implemented creating the list of dictionaries. 我尚未实现创建字典列表的操作。 I tried to start out with the basic and reading each row and copying the data but the output is an empty list. 我尝试从基本开始,读取每一行并复制数据,但输出为空列表。 PLEASE NOTE THAT THIS SEGMENT OF CODE IS WRONG AND ONLY WHAT I TRIED. 请注意,此段代码是错误的,仅是我尝试过的。

def get_rr(self, file_name, path):
    my_lst = []
    #takes every file in folder and put in files list
    for f in os.listdir(path):
        #splits the file name into file name and its extension
        with open(os.path.join(path,f)) as file_object:
                line = file_object.readline()
                while 1:
                    line = file_object.readline().rstrip()
                    if line == "":
                        break
                    my_lst.append(line)

        return my_lst

MY QUESTION: HOW CAN I PROGRAM TO MAKE THE OUTPUT LOOK LIKE BELOW: 我的问题:如何编程使输出外观如下:

output = [
   {"pairs": (1,137), "distance": 7.887},

   {"pairs": (10,127), "distance": 7.388},

   {"pairs": (10,130), "distance": 5.087},

   {"pairs": (11,131), "distance": 5.400},

   {"pairs": (10,134), "distance": 4.770},

]

comprehensions are very handy. 理解非常方便。 Also, you probably don't need the distinction between filepath and name, the OS will handle that for you. 另外,您可能不需要区分文件路径和名称,操作系统将为您处理。 With that in mind something like this should work 考虑到这一点,这样的事情应该工作

def get_ss(self, path):
    with open(path) as file:
        lines = list(file)[2:]
        return [{"pairs":tuple(map(int, words[:2])), "distance":float(words[-1])} for words in (line[:-1].split() for line in lines if line.strip())]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM