Python解析出字符串的一部分

时间:2016-01-16 17:49:24

标签: python parsing

我有一个字符串,如下所示:

场景1:" some_field =(parameter_A-0-(8.869834109E-05))/(0.001 * 10)"

场景2:" some_field =(parameter_A-(0.0005883943))/(0.001 * 10)"

如何解析十进制格式的数字如下所示?

场景1: 第一个数字:-0.00008869834109 第二个数字:0.01

场景2: 第一个数字:0.0005883943 第二个数字:0.01

字符串格式保持不变,但数字格式和极性可能会发生变化。

1 个答案:

答案 0 :(得分:1)

我认为主要工作是从所有周围的角色中提取真正包含数字的碎片。这可以通过.split().find().rfind()和字符串中的索引来完成。

我的代码假定只有一个等号'=',数字部分用'/'分隔,每个用圆括号括起来(如果有多于括号级别,则在最里面),并且可能是最内侧支架左侧的标志。

content = "some_field=(parameter_A-0-(8.869834109E-05))/(0.001*10)"
content = "some_field=(parameter_A-(0.0005883943))/(0.001*10)"
#or which way ever you give the character strings

content = content.split('=')  #split at equal sign
if(len(content) != 2) :  #check for exactly one equal sign
  print ('something wrong in the data?') 

#in case information left of equal sign is needed
fieldname = content[0]

content = content[1]  #now work on the part which is right of '='
content = content.split('/')

values = []
for i in range(len(content)):
  x = content[i]

  pos_open  = x.rfind( '(' ) #find position of opening bracket '(', starting from right--> finds the right-most
  pos_close = x.find( ')' )
  #hence, the digits are in x[pos_open+1:pos_close]  Check this by uncommenting the following line
  #print( x[pos_open+1:pos_close]  )

  #check whether there is a multiplication included in that part
  if ( x[pos_open+1:pos_close].find('*') < 0 ) :    # .find() returns -1 if the character sequence is not found
    val = float(  x[pos_open+1:pos_close]   )    # float() does the main work of conversion 
  else:
    pos_dot = x[pos_open+1:pos_close].find('*')
    factor1 = float(  x[pos_open+1:  pos_open+1 + pos_dot]  )
    factor2 = float(  x[pos_open+1 + pos_dot+1 : pos_close] )
    val = factor1 * factor2

  #check for negative sign:  (sorry, your examples do not show clearly how the sign is specified)
  if (pos_open > 0 and x[pos_open - 1] == '-'):  # this checks the character before the bracket
    #Note: in case of pos_open=0, the second part x[pos_open - 1] would look at the LAST character x[-1]
    val = -val

  values.append(val)

#output
print ('First: {0}  Second: {1}' .format(values[0], values[1]) ) #standard way
print ('First: ' + repr(values[0]) + '  Second: ' + repr(values[1]) ) #another standard way

print ('First: {0:.12f}  Second: {1:.7f}' .format(values[0], values[1]) ) # with format specified in two exemplary ways

如果字符串中存储了两个以上的数字,此代码的提取部分也将起作用,因为它一次抓取一组字符并将确定的值val附加到列表{{ 1}}。

相关问题