0

I have a string with contains numeric values which are inside quotes. I need to remove numeric values from these and also the [ and ]

sample string: texts = ['13007807', '13007779']

texts = ['13007807', '13007779'] 
texts.replace("'", "")
texts..strip("'")

print texts 

# this will return ['13007807', '13007779']

So what i need to extract from string is:

13007807
13007779
3
  • look like you need int() Commented Jul 17, 2019 at 13:47
  • 1
    Umm... does numbers = [int(s) for s in texts] do what you're after (given your sample)? Or is texts actually a string itself? Commented Jul 17, 2019 at 13:47
  • texts was a list i converted to a string texts = str(re.findall(r"\d+", texts)) Commented Jul 17, 2019 at 13:53

3 Answers 3

1

If your texts variable is a string as I understood from your reply, then you can use Regular expressions:

import re
text = "['13007807', '13007779']"
regex=r"\['(\d+)', '(\d+)'\]"
values=re.search(regex, text)
if values:
    value1=int(values.group(1))
    value2=int(values.group(2))

output:

value1=13007807

value2=13007779

Sign up to request clarification or add additional context in comments.

Comments

1

You can use * unpack operator:

texts = ['13007807', '13007779']
print (*texts)

output:

13007807 13007779

if you have :

data = "['13007807', '13007779']"
print (*eval(data))

output:

13007807 13007779

Comments

0

The easiest way is to use map and wrap around in list

list(map(int,texts))

Output

[13007807, 13007779]

If your input data is of format data = "['13007807', '13007779']" then

import re
data = "['13007807', '13007779']"
list(map(int, re.findall('(\d+)',data)))

or

list(map(int, eval(data)))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.