从csv获取数据并从中获取带有整数的变量

时间:2018-11-30 19:39:55

标签: python list csv matrix vector

我有一组包含整数的列表,我想将变量列表从列表转换为整数

到目前为止,这是我的代码:

from csv import reader
from collections import defaultdict
from ast import literal_eval
from pprint import pprint

data = defaultdict(list)
with open('crystal.csv') as f:
    csv_reader = reader(f)

current_header = None
for line in csv_reader:

    # We found a header
    if len(line) == 1 and all(item.isalpha() or item.isspace() for item in line[0]):
        current_header = line[0]

    # Otherwise normal line with ints and floats
    else:
        data[current_header].append(list(map(literal_eval, line)))

pprint(data)

mult=data['multiplicty']
lattice=data['lattice parameters']
occup=data['occupancy']
sites=data['atom sites']

我的csv文件是:

multiplicty
4.123
lattice parameters
1,0,0
0,1,0
0,0,1
atom sites
0,0,0
0.5,0.5,0.5
1,1,1
occupancy
1,0,0
0,1,0
0,0,1

理想情况下,我希望输出如下:
grid = 4.123

晶格参数= [1,0,0; 0,1,0; 0,0,1]矩阵

0 个答案:

没有答案