我的文本文件是一个庞大的数据列表(很大意思,我不能手工格式化)只包含数字,格式如下:
1 5555 6666
2 5555 6666
1 7755 6666
3 8888 6666
我想将前两列用作我的键,将第三列用作其值。
这是我的代码:
import string
def load (filename):
with open ('filename', 'r'):
dict = {}
for line in file.readline():
key, site, value = dict([line.strip().split('\t')for line in file
dict[key[0]]+[site[1]]= value[2]
return dict
但是我的代码失败了。
我想要的输出是:
{('1', '5555'): '6666', ('2', '5555'): '6666', ('1', '7755'): '6666', ('3', '8888'): '6666'}
有可能实现我的输出吗?我是在正确的轨道上吗?如果没有,我哪里出错了,我该如何解决?
谢谢
答案 0 :(得分:1)
您可以使用csv模块读取按照您传递的分隔符分割元素的内容,然后解压缩并使用元组中的前两个元素作为键,将最后一个元素用作值:
import csv
with open("in.csv") as f:
d = {}
r = csv.reader(f, delimiter=" ") # pass whatever your delimiter is
for row in r: # first row 1 5555 6666 -> ["1", "5555", "6666"]
a, b, v = row # a,b,c = "1", "5555", "6666"
d[(a, b)] = v # create a tuple from the first two elements of the row
print(d)
{('3', '8888'): '6666', ('1', '5555'): '6666', ('1', '7755'): '6666', ('2', '5555'): '6666'}
如果您想要订购的数据,请使用OrderedDict:
import csv
from collections import OrderedDict
with open("in.csv") as f:
d = OrderedDict()
r = csv.reader(f, delimiter=" ")
for row in r:
a, b, v = row
d[(a, b)] = v
print(d)
如果您有可能重复密钥,那么您需要将值存储在列表或某个容器中:
import csv
from collections import OrderedDict
with open("in.csv") as f:
d = OrderedDict()
r = csv.reader(f, delimiter=" ")
for row in r:
a, b, v = row
d.setdefault((a,b),[]).append(v)
print(d)
你自己的代码有多个错误:
def load(filename):
with open(filename, 'r') as f: # as f and pass variable filename not a string
d = {} # don't shadow the python dict
for line in f: # iterate over the file object
key, site, value = line.split() # unpack
d[(key, site)] = value # already unpacked so just use the variables
return d
然后调用你的函数传递文件名:
print(load("in.csv"))
{('1', '5555'): '6666', ('3', '8888'): '6666', ('2', '5555'): '6666', ('1', '7755'): '66`66'}
答案 1 :(得分:1)
您不应重新定义内置类型dict
。你应该编写正确的python代码:
def load(filename):
with open('filename', 'r') as inp:
result = {}
for line in inp:
key, site, value = line.strip().split('\t')
result[key,site] = value
return result