在python中将数据从文件导入到字典

时间:2016-01-24 13:02:59

标签: python python-3.x

我想将文件导入字典以进行进一步处理。该文件包含NLP的嵌入向量。它看起来像:

the 0.011384 0.010512 -0.008450 -0.007628 0.000360 -0.010121 0.004674 -0.000076 
of 0.002954 0.004546 0.005513 -0.004026 0.002296 -0.016979 -0.011469 -0.009159 
and 0.004691 -0.012989 -0.003122 0.004786 -0.002907 0.000526 -0.006146 -0.003058
one 0.014722 -0.000810 0.003737 -0.001110 -0.011229 0.001577 -0.007403 -0.005355

我使用的代码是:

embeddingTable = {}

with open("D:\\Embedding\\test.txt") as f:
    for line in f:
       (key, val) = line.split()
       d[key] = val
print(embeddingTable)

错误:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-3612e9012ffe> in <module>()
 24 with open("D:\\Embedding\\test.txt") as f:
 25     for line in f:
---> 26        (key, val) = line.split()
 27        d[key] = val
 28 print(embeddingTable)

ValueError: too many values to unpack (expected 2)

据我所知,它预计2个值不是9,但是有可能将word作为键和矢量作为值插入吗?

3 个答案:

答案 0 :(得分:6)

您需要使用* operator

embeddingTable = {}
with open("D:\\Embedding\\test.txt") as f:
    for line in f:
       key, *values = line.split() # fix here
       embeddingTable[key] = [float(value) for value in values]
print(embeddingTable)

答案 1 :(得分:3)

如果由于使用的是Python 2而无法使用*运算符,则可以执行以下操作:

embeddingTable = {}
with open('test.txt') as f:
    for line in f:
       values = line.split()
       embeddingTable[values[0]] = values[1:]
print(embeddingTable)

如果您使用的是Python 3,请使用更优雅的*运算符。

答案 2 :(得分:3)

使用csv lib解析解压缩并使用 dict comp 解析map个值到浮点数:

import csv

with open("D:/Embedding/test.txt") as f:
    d = {k:list(map(float, vals)) for k, *vals in csv.reader(f,delimiter=" ")}