【发布时间】:2015-06-19 20:39:04
【问题描述】:
我正在尝试在 python 中编写一个解析器来读取输入文件,然后将结果组装成几个数组。
数据结构如下:
Some_numbers
1
5
6
some_vector
[-0.99612937 -0.08789929 0. ]
[-0.99612937 -0.08789929 0. ]
[ -9.99999987e-01 1.61260621e-04 0.00000000e+00]
Some_data
1239 #int
671
471
851
S4RS #string
517
18
48
912
S4RS
目前我尝试过的方法有:
text_file = 'C:\AA\aa.txt'
lines = open(text_file).read().splitlines()
numbers = []
Vector = []
for line in lines:
line = line.strip()
if line.startswith('Some_numbers'):
continue
numbers.append(line)
if line.startswith('some_vector'):
continue
Vector.append(line)
我遇到的问题是: 1) 有多个分隔符 2)尝试根据相关部分拆分数据
我还尝试使用 np.genfromtxt 以及无数小时在互联网上拖网。
非常感谢您的 cmets 和建议。
【问题讨论】:
标签: python arrays string parsing delimiter