1

我有一个文件,其中包含从 MD 模拟包生成的坐标。

格式如下:

(1.323232, 1.22323, 3.23123)

您还可以指定坐标的输出,如下所示,不带逗号:

(1.323232 1.22323 3.23123)

如何使用 python 解析数组中的这样一行。具体如何删除括号。在 C 中,使用 scanf 很容易做到这一点?

4

4 回答 4

1

你可以这样做:

txt=['(1.323232, 1.22323, 3.23123)',
      '(1.32.3232, 1.22323, 3.23123)',
      '(1.323232 1.22323 3.23123)']

data=[]  
for i, s in enumerate(txt):
    st=s.strip().lstrip('(').rstrip(')')  
    if ',' in s:
        res=[e.strip() for e in st.split(',')]
    else:
        res=st.split()
    try:    
        res=map(float, res) 
    except ValueError:
        print 'Element {} "{}" is invalid'.format(i,s)    
        continue   

    data.append(res)   

print 'data:', data

印刷:

Element 1 "(1.32.3232, 1.22323, 3.23123)" is invalid
data: [[1.323232, 1.22323, 3.23123], [1.323232, 1.22323, 3.23123]]
于 2013-10-30T19:22:11.173 回答
1

有两种明显的方法可以做到这一点:使用或不使用正则表达式。由于您可能会得到 300 个正则表达式答案,所以让我们展示如何在没有的情况下做到这一点。

我们想去掉括号,然后在空格或逗号上分割,然后是空格。另一种说法是:去掉括号,然后用空格分割,然后去掉可选的尾随逗号。例如:

line = line[1:-1] # strip the parens
bits = line.split() # split on whitespace
bits = [bit.rstrip(',') for bit in bits] # strip trailing commas
bits = map(float, bits) # convert to float

您当然可以将这一切合并为一行:

bits = [float(bit.rstrip(',')) for bit in line[1:-1].split()]
于 2013-10-30T19:19:32.997 回答
1

我打算写一些类似其他答案的东西,但是,为了完整起见,我将成为正则表达式的人

import re
f = open('myfile', 'r')
r = re.compile(r'\-*\d+\.*\d+')
data =[]
for line in f:
    data.append(map(float, r.findall(line)))
于 2013-10-30T19:26:56.587 回答
0

把 re 改成

r'\-*\d+\.*\d*'

还将捕获单个数字,例如 (1, 2, 3)

import re
f = open('myfile', 'r')
r = re.compile(r'\-*\d+\.*\d*')
data =[]
for line in f:
    data.append(map(float, r.findall(line)))
于 2014-04-28T23:05:59.097 回答