我有一个具有这种格式的输入文件
{A}
4.240000 1.593631 1.593631
4.240000 -1.593631 1.593631
4.240000 -1.593631 -1.593631
4.240000 1.593631 -1.593631
{B}
-4.240000 -1.593631 -1.593631
-4.240000 1.593631 -1.593631
{C}
...
我想读{A},保存下面的数组,使用该数组,然后移到{B}进行相同的操作,...
我有这样的东西
import sys
#Read file
inFile = sys.argv[1]
with open(inFile) as vfile:
line = vfile.readline()
while line:
if line.find("{") == 0:
A = line.split('\n')
else:
line.split()
array = []
line = [int(i) in line]
array.append(line)
print(A, array)
答案 0 :(得分:0)
import re
data = {}
key = None
for line in file:
line = line.strip()
if line.endswith('}'):
key = line[1:-1]
data[key] = []
elif line and key is not None:
# if the number of spaces can be different
values = re.split('\s+', line)
data[key].extend(map(float, values))
您将获得如下数据:
{'A': [4.24, 1.593631, 1.593631, ...], 'B': [-4.24, -1.593631, -1.59, ...] ...}