Skip to content Skip to sidebar Skip to footer

Removing Parentheses And Comma

I'm importing Data from a database into python data frame. Now, I wish to use the data for further analysis, however, I need to do a little cleaning of the data before using. Curre

Solution 1:

This is one way to do it:

import re
x = "'('2275.1', '1950.4')'"
y = re.findall(r'\d+\.\d', x)
for i in y:
  print i

Output:

2275.11950.4

Solution 2:

Try ast.literal_eval, which evaluates its argument as a constant Python expression:

import ast

data = ast.literal_eval("('2275.1', '1950.4')")
# datais now the Python tuple ('2275.1', '1950.4')

x, y = data
# x is'2275.1' and y is'1950.4'

Solution 3:

import re
print re.findall(r"\b\d+(?:\.\d+)?\b",test_str)

You can simply do this.

or

printmap(float,re.findall(r"\b\d+(?:\.\d+)?\b",x))

If you want float values.

Solution 4:

I assume, that the string you provided is actually the output of python. It is hence a tuple, containing two strings, which are numbers. If so and you would like to replace the ', you have to convert them to a number format, such as float:

a = ('2275.1', '1950.4')
a = [float (aI) for aI in a] 
print a
[2275.1, 1950.4]

Solution 5:

Here a non-regex approach:

data = (('2275.1', '1950.4'))


result = data[0]# 0 means the value in the first row
result2 = data[1]# 1 means the next row after 0print result
print result2

Output:

>>> 
2275.1
1950.4
>>>

Post a Comment for "Removing Parentheses And Comma"