从此.txt文件创建字典{names:values of values}?

时间:2018-03-10 16:14:49

标签: python-3.x

我有一个.txt文件,其名称后跟数字。例如

namesToRatings = {}

with open("example.txt") as document:
    for line in document:
        print(line)

输出:

Simon

5 0 0 0 0 0 0 1 0 1 -3 5 0 0 0 5 5 0 0 0 0 5 0 0 0 0 0 0 0 0 1 3 0 1 0 -5 0 
0 5 5 0 5 5 5 0 5 5 0 0 0 5 5 5 5 -5 

John

5 5 0 0 0 0 3 0 0 1 0 5 3 0 5 0 3 3 5 0 0 0 0 0 5 0 0 0 0 0 3 5 0 0 0 0 0 5 
-3 0 0 0 5 0 0 0 0 0 0 5 5 0 3 0 0 

依旧......

如何创建一个字典,其中键是人名,值是该名称后面的数字列表?

E.g {Simon:[5,0,0,0,0,......... 5,5,-5]

4 个答案:

答案 0 :(得分:2)

with open("example.txt") as document:
    lines = [line for line in document if len(line.strip())]
    namesToRatings = {lines[i] : lines[i+1].split(" ") for i in range(0, len(lines), 2)}
    print(namesToRatings)  # print it, return it from a function, or set it as a global if you really must.

答案 1 :(得分:1)

您可以使用regex

import re

di={}
with open('file.txt') as f:
    for m in re.finditer(r'^([a-zA-Z]+)\s+([-\d\s]+)', f.read(), re.M):
        di[m.group(1)]=m.group(2).split()

答案 2 :(得分:1)

尝试在差异库上使用get_close_matches。

将字典单词和含义保存在JSON文件中,然后它将在python字典的Form中。

import json

from difflib import get_close_matches


data=json.load(open('filePath'))

def check_word(word) :
        word=word.lower()
         for word in data:
         return data
         If len(get_close_matches(word, data.keys(), cutoff=0.7))>0:
          return get_close_matches(word, data.keys(), cutoff=0.7)
  

您可以在此处添加更多例外...

答案 3 :(得分:0)

with open("example.txt") as document:
    lines = (line.strip() for line in document)
    lines = (line for line in lines if line)
    pairs = zip(*[lines]*2)
    namesToRatings = {name: [int(x) for x in values.split()] for name, values in pairs}

此版本与John的答案中概述的基于列表的方法类似,但不需要将整个文件读入列表。 zip(*[lines]*2)会将输入(行)分成两对。输出:

{
 'Simon': [5, 0, 0, 0, 0, 0, 0, 1, 0, 1, -3, 5, 0, 0, 0, 5, 5, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 0, 1, 0, -5, 0, 0, 5, 5, 0, 5, 5, 5, 0, 5, 5, 0, 0, 0, 5, 5, 5, 5, -5], 
 'John': [5, 5, 0, 0, 0, 0, 3, 0, 0, 1, 0, 5, 3, 0, 5, 0, 3, 3, 5, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 3, 5, 0, 0, 0, 0, 0, 5, -3, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 5, 5, 0, 3, 0, 0]
}