Reputation: 925
I have a line which looks like this:
line ='timing [dash<try></example><try>x</trial>] -122 µm'
I want to extract the -122 value.This is what I have written:
a = line.split("]")
b = a[1].split("x")
c= b[0].split("µm")
my_val = float(c[0].replace(" ", ""))
The "µ" seems to cause a problem here. And above code shows error if I compile it. Is there any idea to work with those special characters?
Small Edit:
I am reading the "line" from some file. I am using python 2.7.
Upvotes: 0
Views: 255
Reputation: 196
Look at Python + Unicode tutorial. At first if you have any unicode characters use u e.g. It is safe to use u' ... ' for every string in your code.
line = u'timing....'
c= b[0].split(u"µm")
Upvotes: 1
Reputation: 8702
import re
line ="asdfasd;l lakfdalskd -122 asfdsd"
print re.findall(r'-\d+', line)
Upvotes: 0
Reputation: 43265
Works fine in python 2.7
>>> line ='timing [dash<try></example><try>x</trial>] -122 µm'
>>> a = line.split("]")
>>> b = a[1].split("x")
>>> c= b[0].split("µm")
>>> a
['timing [dash<try></example><try>x</trial>', ' -122 \xc2\xb5m']
>>> b
[' -122 \xc2\xb5m']
>>> c
[' -122 ', '']
>>> c[0].replace(" ", "")
'-122'
>>> c[0].strip()
'-122'
>>> float(c[0].replace(" ", ""))
-122.0
Upvotes: 1