Reputation: 89
I am new to python and I have a string that looks like this
Temp = "', '/1412311.2121\n
"
my desired output is just getting the numbers and decimal itself.. so im looking for
1412311.2121
as the output.. trying to get rid of the ', '/\n in the string.. I have tried Temp.strip("\n") and Temp.rstrip("\n") for trying to remove \n but i still seems to remain in my string. :/... Does anyone have any ideas? Thanks for your help.
Upvotes: 3
Views: 48975
Reputation: 24788
Strings are immutable. string.strip()
doesn't change string
, it's a function that returns a value. You need to do:
Temp = Temp.strip()
Note also that calling strip()
without any parameters causes it to remove all whitespace characters, including \n
As stalk said, you can achieve your desired result by calling strip("',/\n")
on Temp
.
Upvotes: 18
Reputation: 13510
If the data are like you show, numbers that are wrapped from right and left with non-number data, you can use a very simple regular expression:
g = re.search('[0-9.]+', s) # capture the inner number only
print g.group(0)
Upvotes: 6
Reputation: 500495
I would use a regular expression to do this:
In [8]: s = "', '/1412311.2121\n"
In [9]: re.findall(r'([+-]?\d+(?:\.\d+)?(?:[eE][+-]\d+)?)', s)
Out[9]: ['1412311.2121']
This returns a list of all floating-point numbers found in the string.
Upvotes: 6