从字符串中隔离数据

时间:2016-01-23 19:17:21

标签: python string

下面的字符串会在我的3d应用程序中打印出来,它是几何的位置数据:

((1,0,0)(0,1,0)(0,0,1)(0,0,0)),((1,0,0)(0,1,0)( 0,0,1)(-38.805,0,1333.283)),((1,0,0)(0,1,0)(0,0,1)( - 77.609,0,2666.566)),(( 1,0,0(0)(0,1,0)(0,0,1)( - 116.414,0,3999.849)),((1,0,0)(0,1,0)(0,0, 1)( - 155.218,0,5333.133))......

我只希望字符串打印以下内容: (0,0,0),( - 38.805,0,1333.283),( - 77.609,0,2666.566),( - 116.414,0,3999.849),( - 155.218,0,5333.133)...

1 个答案:

答案 0 :(得分:0)

此代码:

inp = "((Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (0, 0, 0)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-38.805, 0, 1333.283)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-77.609, 0, 2666.566)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-116.414, 0, 3999.849)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-155.218, 0, 5333.133)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-194.023, 0, 6666.416)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0) ; v3: (0, 0, 1); off: (-232.827, 0, 7999.699)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-271.632, 0, 9332.982)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-310.436, 0, 10666.265)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-349.241, 0, 11999.548)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-388.045, 0, 13332.832)),Matrix(v1: (1, 0, 0); v2: (0, 1, 0); v3: (0, 0, 1); off: (-426.85, 0, 14666.115))"

lst = inp.split(";")
for s in lst:
    s.strip()
    if s.find("off:") > -1:
        data = s.split(")")
        print(data[0][7:])

将产生:

0, 0, 0
-38.805, 0, 1333.283
-77.609, 0, 2666.566
-116.414, 0, 3999.849
-155.218, 0, 5333.133
-194.023, 0, 6666.416
-232.827, 0, 7999.699
-271.632, 0, 9332.982
-310.436, 0, 10666.265
-349.241, 0, 11999.548
-388.045, 0, 13332.832
-426.85, 0, 14666.115

注意:我没有使用列表理解或生成器 - 使代码尽可能简单。