使用您在pastebin上提供的字符串(作为变量a的内容):>>> result = [i.strip().split('\n') for i in a.split('Sequence')]
>>> [len(i) for i in result]
[10, 1, 3, 1, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 23, 24, 3, 1, 23, 23, 23]
例如:
^{pr2}$
所以我们只过滤那些有23个元素的元素(注意第一个元素是:):>>> result = [i[1:] for i in result if len(i) == 23]
>>> [len(i) for i in result]
[22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22]
现在你有一个数组,看起来像这样:>>> print( '\n'.join(result[0]))
Value(int): 10564
Value(int): 13
Value(int): 388
Value(int): 0
Value(int): -321
Value(int): 83
Value(string): 272
Value(string): 05
Value(int): 67
Value(int): 67
Value(int): 708
Value(int): 896
Value(int): 31
Value(int): 128
Value(int): -12
Value(int): -109
Value(int): 0
Value(int): -20
Value(int): -111
Value(int): -1
Value(int): -1
Value(int): 0
因此,您所提供的数据所需的全部代码是:proc_stdout = proc.communicate(ran_opt_get_access_data)[0].decode('utf-8')
result = [i.strip().split('\n') for i in proc_stdout.split('Sequence')]
result = [i[1:] for i in result if len(i) == 23]
# Or at least [i[1:] for i in result if len(i) > 1]
编辑-删除值
我们将使用简单的hack,因此始终只有一个:和^{}和{a2}来删除空白:def filter_value(text):
index = text.find( ':')
# Not found :
if index < 0:
return text.strip()
return text[index+1:].strip()
并通过替换这一行来实现:result = [i[1:] for i in result if len(i) == 23]
有了这一层:result = [[filter_value(j) for j in i[1:]] for i in result if len(i) == 23]