如何将文本文件上传到字典中(在Python中)?

时间:2014-03-22 16:35:10

标签: python dictionary

我正在尝试创建一种算法,向用户提供个性化的图书推荐,根据同行的分数预测用户可能会喜欢哪些图书。

用于解释的两个文本文件是“ratings.txt”和“books.txt”,它们表示如下:

ratings.txt “Ben5 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 0 0 0 0 0 5 5 0 5 5 5 0 5 5 0 0 0 5 5 5 5 -5 Moose5 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 0 5 0 0 0 0 0 0 5 5 0 3 0 0 ...“(代表两个用户)

books.txt “道格拉斯亚当斯,银河系的漫游指南 Richard Adams,Watership Down Mitch Albom,你在天堂遇见的五个人 Laurie Halse Anderson,说话 Maya Angelou,我知道为什么笼中的鸟唱“(四本书和作者代表)

评级系统从-5(真的不喜欢它)到0(没读过它)到5(非常喜欢它)

我如何将这两个文件作为字典上传到Python用于此相似度算法?

提前谢谢。

4 个答案:

答案 0 :(得分:0)

读取文件,逐行迭代,每行split。关键是具有0索引的值,值 - 索引为1及更多的项目列表:

data = {}
with open('test.txt', 'r') as f:
    for line in f:
        line_data = line.split()
        data[line_data[0]] = line_data[1:]

print data

打印:

{'Ben5': ['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'], 
 'Moose5': ['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']}

另外,如果你想要数字作为整数:

data[line_data[0]] = map(int, line_data[1:])

希望有所帮助。

答案 1 :(得分:0)

你提供的东西很少但需要从某个地方开始。我假设您的文件中的单词是键,其余的是要存储在列表中的值我不会看到新行,如果每个观察都是由换行符定义,那么alecxe的解决方案可以工作

我正在对此进行修改,以输入文件作为字符串读取,并为每一步添加了一个strip()方法,以确保占用换行符

from collections import defaultdict

test = open(someFile).read()  # this 

mydict = defaultdict(list)
from collections import defaultdict
for item in test.split():
    try:
        x = int(item.strip())
        mydict[currentKey].append(x)
    except ValueError:
        currentKey = item.strip()
        mydict[currentKey] = []


defaultdict(<type 'list'>, {'Ben5': [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], 'Moose5': [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]})

答案 2 :(得分:0)

使用带有re的正则表达式尝试这个:

import re

### assume read all lines from a file
input = '''Ben5 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 Moose5 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 9

Moos7 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'''

output = {}
for (key, val) in re.findall("([a-zA-Z]\w+)\s+(.*?)(?=[a-zA-Z]|$)", input, re.M):
    output[key] = val

print output

答案 3 :(得分:0)

正则表达式将有助于解决此问题。

您正在寻找两种不同的模式 -

import re
s = "Ben5 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 Moose5 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"

name = '(\D+)'
ratings = '((?:-?\d\s)+)'

将它们放在一起,整体模式将与两组相匹配。使用单个模式执行大量匹配时,可能值得编译模式 -

pattern = name + ratings
regex = re.compile(pattern)

您可以迭代字符串中的匹配并构建字典 piecemeal -

d = dict()

for match in regex.finditer(s):
    name, ratings = match.groups()
    print name, ':', ratings
    print '*'*8
    d[name] = ratings

print d

>>> 
Ben : 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 
********
Moose : 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 
********
{'Moose': '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 ', 'Ben': '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 '}

或者您可以一次构建词典 -

d = dict(regex.findall(s))
相关问题