我認爲主要工作是提取真正包含所有周圍字符數字的棋子。這可以通過.split()
,.find()
,.rfind()
和字符串內的索引來完成。
我的代碼假設只有一個等號'=',數字部分之間用'/'分隔,每個用圓括號括起來(如果多於括號級別,則在最裏面)可能是直接位於最內層支架左側的符號。如果有存儲在字符串多於兩個數字,因爲它抓住一個字符組的時間和附加所確定的值val
到列表values
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
此代碼的提取部分也將起作用。
你應該首先展示你已經試圖解決你的問題和你被困在哪裏。 – ForceBru
如何使用[正則表達式](https://docs.python.org/2/library/re.html)? –
你想要的結果是一個字符串,一個'浮動'(這是一個二進制浮點數,只會近似十進制浮點數)或可能是'decimal.Decimal'?爲什麼第一個數字是負數,第二個數字是正數? – tdelaney