I have string in the following form: - my_string = '(1, 2) (3, 4)'
As you can see, the tuples inside the string are space separated. I want to know how can I extract those tuples into separate variables.
3 1858 bvdet 2,851
Expert Mod 2GB
There may be a better way. - >>> my_string = '(1, 2) (3, 4)'
-
>>> eval(my_string.replace(") (", "),("))
-
((1, 2), (3, 4))
-
>>> a,b = eval(my_string.replace(") (", "),("))
-
>>> a
-
(1, 2)
-
>>> b
-
(3, 4)
-
>>>
Thank you bvdet. But I have a question.
Can you please explain me what is happening in the code ?? I have never used eval function in Python.
Also I have read that using eval is not recommended in Python. Can you tell me why is that ??
EDIT: I used your approach of substituting the
part with
so that I get the string
Then after that, I searched on StackOverflow about ast module and literal_eval function. converting-string-to-tuple
I used it to separate the tuples into variables.
Thank You for the help .
bvdet 2,851
Expert Mod 2GB
Built-in funcation eval: Python documentation for eval
The function could potentially evaluate malicious code, therefore its use if sometimes frowned upon. A regex solution could potentially be a more robust but complicated solution. - >>> import re
-
>>> re.compile("[(](\d+, \d+)[)]")
-
<_sre.SRE_Pattern object at 0x0000000005C3EA80>
-
>>> pattern = re.compile("[(](\d+, \d+)[)]")
-
>>> pattern.findall('(1, 2) (3, 4)')
-
['1, 2', '3, 4']
-
>>> ((int(i) for i in item.split(",")) for item in pattern.findall('(1, 2) (3, 4)'))
-
<generator object <genexpr> at 0x0000000005E7CD38>
-
>>> for obj in ((int(i) for i in item.split(",")) for item in pattern.findall('(1, 2) (3, 4)')):
-
... print tuple(obj)
-
...
-
(1, 2)
-
(3, 4)
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: AMB |
last post by:
Hi all,
I'm currently working on a large project which uses XML formatted data
to communicate between all the various different systems the project
ties together, apart from one, which...
|
by: Ryan |
last post by:
I am trying to write a c# function to extract all numbers out of a string.
What is the easiest way to do this, regular expressions?
I must account for numbers formatted with or without commas...
|
by: Maya |
last post by:
Hello guys,
Is there an easy way to extract individual words that form a string and
store them in variables like in this example:
String "how are you?"
My result would be:
var1 = "how"
|
by: deko |
last post by:
If I have random and unpredictable user agent strings containing URLs, what is
the best way to extract the URL?
For example, let's say the string looks like this:
registered NYSE 943 <a...
|
by: Helmut Jarausch |
last post by:
Hi,
I'm looking for an elegant solution to the following (quite common)
problem:
Given a string of substrings separated by white space,
split this into tuple/list of elements.
The problem...
|
by: Phantom |
last post by:
I'm wondering if anyone knows a trick to iterate a FLWR expression
across a literal string that is delimited by spaces.
e.g.
for $x in ("apple orange banana blueberry")
obviously, this...
|
by: ganesh gajre |
last post by:
Hi all,
I want to read file which is mapping file. Used in to map character from ttf
to unicode.
eg
Map file contain data in the following way:
0 ०
1 १
|
by: Rootz88 |
last post by:
ok I have this string and other similar( ull find them in the attachment)
"BRUNEI BANDAR SERI BEGAWAN 5770 343653"
I need to extract BRUNEI, BANDAR SERI...
|
by: Robert Hodan |
last post by:
So, I'm trying to make a method that takes an input stream (from a text file), and churns out each word into it's own string, and sticks it into a list. According to my logic this should work, but...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |