Python中CSV文件中的二维字典、列表或数组
我对python很陌生 我正在尝试从以下格式的csv文件将回归系数矩阵读入python:Python中CSV文件中的二维字典、列表或数组,python,file,csv,dictionary,Python,File,Csv,Dictionary,我对python很陌生 我正在尝试从以下格式的csv文件将回归系数矩阵读入python: 0.10 0.15 0.20 0.25 0.30 0.35 a1 -0.0011 0.0008 0.0019 0.0034 0.0067 0.0047-0.0026 a2 0.0134 -0.3042 -0.2531 -0.2138 -1.2345 -0.2380 2.0402 a3 0.0546 0.2708 0.1738 0.0810 0.8451 -0.0034 -1.4961 a4 -0.
0.10 0.15 0.20 0.25 0.30 0.35
a1 -0.0011 0.0008 0.0019 0.0034 0.0067 0.0047-0.0026
a2 0.0134 -0.3042 -0.2531 -0.2138 -1.2345 -0.2380 2.0402
a3 0.0546 0.2708 0.1738 0.0810 0.8451 -0.0034 -1.4961
a4 -0.0226 -0.0052 -0.0021 -0.0024 -0.0023 -0.0745 0.0563
a5 -0.0101 0.0108 0.0153 0.0263 0.0491 0.0327 -0.0691
我需要能够访问该矩阵的特定元素,例如a['a1','0.10']=-0.0011。我认为dict适合存储这些数据,但发现很难使其成为二维的
我已经设法将这些数据读入字典,并将最上面的行元素作为键,但我不知道如何实现我想要的双键。我使用的代码如下:
import csv, sys
reader = csv.DictReader(open(sys.path[0]+"\\DSYHScoeff_98.dat", 'r'), delimiter=' ')
result = {}
for row in reader:
for column, value in row.iteritems():
result.setdefault(column, []).append(value)
你有什么好的方法来处理这些数据的建议吗
致以最良好的祝愿,
亚当老实说,我会手工做的
header,data = None,dict()
with open("filename.csv") as f:
for line in f:
if header is None:
header = line.split()
continue
l = line.split()
for i in xrange(len(l)-1):
data[l[0],header[i]] = l[i+1]
一旦我对tobias_k进行了调整,这些调整也在他们的评论中提到了。我要做的可能是在文件的开头添加一些类似“ax”的内容:
ax 0.10 0.15 0.20 0.25 0.30 0.35
a1 -0.0011 0.0008 0.0019 0.0034 0.0067 0.0047 -0.0026
[...]
然后稍微更改代码:
result = {}
for row in reader:
x = row.pop(reader.fieldnames[0])
for column, value in row.iteritems():
if column and value:
y = float(column)
result[x,y] = float(value)
#
# insert your code here
#
from pprint import pprint
pprint(result)
def cell(arr,row,col):
try:
return result[col][result['row'].index(row)]
except KeyError:
return "N/A"
pprint(cell(result, 'a1', '0.10'))
pprint(cell(result, 'a1', '0.14'))
它应该在以下方面发挥作用:
>>> result['a3',0.15]
0.2708
搭配,它是专为这种东西设计的:
>>> import pandas as pd
>>> names = ['0.10', '0.15', '0.20', '0.25', '0.30', '0.35', '0.40']
>>> i = pd.read_csv('test.csv', delim_whitespace=True, names=names)
>>> i
0.10 0.15 0.20 0.25 0.30 0.35 0.40
0 -0.0011 0.0008 0.0019 0.0034 0.0067 0.0047 -0.0026
1 0.0134 -0.3042 -0.2531 -0.2138 -1.2345 -0.2380 2.0402
2 0.0546 0.2708 0.1738 0.0810 0.8451 -0.0034 -1.4961
3 -0.0226 -0.0052 -0.0021 -0.0024 -0.0023 -0.0745 0.0563
4 -0.0101 0.0108 0.0153 0.0263 0.0491 0.0327 -0.0691
>>> i['0.10'][0]
-0.0011000000000000001
您必须首先向第一列添加标签:
# ▼▼▼
row 0.10 0.15 0.20 0.25 0.30 0.35
a1 -0.0011 0.0008 0.0019 0.0034 0.0067 0.0047-0.0026
a2 0.0134 -0.3042 -0.2531 -0.2138 -1.2345 -0.2380 2.0402
# [...]
之后,这只是在“row-column”中获取行索引的问题。包装在函数中:
def cell(arr,row,col):
try:
return result[col][result['row'].index(row)]
except KeyError:
return "N/A"
给定您的输入文件和代码:
result = {}
for row in reader:
x = row.pop(reader.fieldnames[0])
for column, value in row.iteritems():
if column and value:
y = float(column)
result[x,y] = float(value)
#
# insert your code here
#
from pprint import pprint
pprint(result)
def cell(arr,row,col):
try:
return result[col][result['row'].index(row)]
except KeyError:
return "N/A"
pprint(cell(result, 'a1', '0.10'))
pprint(cell(result, 'a1', '0.14'))
制作:
{None: [[''], [''], [''], ['']],
'': ['', '2.0402', '-1.4961', '0.0563', '-0.0691'],
'0.10': ['-0.0011', '0.0134', '0.0546', '-0.0226', '-0.0101'],
'0.15': ['0.0008', '-0.3042', '0.2708', '-0.0052', '0.0108'],
'0.20': ['0.0019', '-0.2531', '0.1738', '-0.0021', '0.0153'],
'0.25': ['0.0034', '-0.2138', '0.0810', '-0.0024', '0.0263'],
'0.30': ['0.0067', '-1.2345', '0.8451', '-0.0023', '0.0491'],
'0.35': ['0.0047-0.0026', '-0.2380', '-0.0034', '-0.0745', '0.0327'],
'row': ['a1', 'a2', 'a3', 'a4', 'a5']}
'-0.0011'
'N/A'
(请注意,您的输入数据文件可能格式不正确;这在
pprint
'ed字典中非常明显——有关详细信息,请参阅您的问题注释)您的标题行似乎太短。它只有6个标签,而所有其他行都有1个标签和7个值。另外,0.0047-0.0026
中真的没有空格吗?我看了一下。此功能可接受51种不同的KWARG。那是世界纪录还是什么?