Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #105596 > unrolled thread
| Started by | "ast" <nomail@com.invalid> |
|---|---|
| First post | 2016-03-24 11:39 +0100 |
| Last post | 2016-03-24 18:47 +0100 |
| Articles | 14 — 8 participants |
Back to article view | Back to comp.lang.python
newbie question "ast" <nomail@com.invalid> - 2016-03-24 11:39 +0100
Re: newbie question David Palao <dpalao.python@gmail.com> - 2016-03-24 11:49 +0100
Re: newbie question "ast" <nomail@com.invalid> - 2016-03-24 11:54 +0100
Re: newbie question Steven D'Aprano <steve@pearwood.info> - 2016-03-25 00:53 +1100
Re: newbie question Grant Edwards <invalid@invalid.invalid> - 2016-03-24 15:05 +0000
Re: newbie question wxjmfauth@gmail.com - 2016-03-24 10:11 -0700
Re: newbie question Matt Wheeler <m@funkyhat.org> - 2016-03-24 10:57 +0000
Re: newbie question Tim Chase <python.list@tim.thechases.com> - 2016-03-24 05:58 -0500
Re: newbie question "Sven R. Kunze" <srkunze@mail.de> - 2016-03-24 12:10 +0100
Re: newbie question Steven D'Aprano <steve@pearwood.info> - 2016-03-24 22:13 +1100
Re: newbie question Matt Wheeler <m@funkyhat.org> - 2016-03-24 13:22 +0000
Re: newbie question "ast" <nomail@com.invalid> - 2016-03-28 17:34 +0200
Re: newbie question "Sven R. Kunze" <srkunze@mail.de> - 2016-03-29 12:23 +0200
Re: newbie question "Sven R. Kunze" <srkunze@mail.de> - 2016-03-24 18:47 +0100
| From | "ast" <nomail@com.invalid> |
|---|---|
| Date | 2016-03-24 11:39 +0100 |
| Subject | newbie question |
| Message-ID | <56f3c3eb$0$4546$426a74cc@news.free.fr> |
Hi I have a string which contains a tupe, eg: s = "(1, 2, 3, 4)" and I want to recover the tuple in a variable t t = (1, 2, 3, 4) how would you do ?
[toc] | [next] | [standalone]
| From | David Palao <dpalao.python@gmail.com> |
|---|---|
| Date | 2016-03-24 11:49 +0100 |
| Message-ID | <mailman.86.1458816553.2244.python-list@python.org> |
| In reply to | #105596 |
Hi, Use "eval": s = "(1, 2, 3, 4)" t = eval(s) Best 2016-03-24 11:39 GMT+01:00 ast <nomail@com.invalid>: > Hi > > I have a string which contains a tupe, eg: > > s = "(1, 2, 3, 4)" > > and I want to recover the tuple in a variable t > > t = (1, 2, 3, 4) > > how would you do ? > > > -- > https://mail.python.org/mailman/listinfo/python-list
[toc] | [prev] | [next] | [standalone]
| From | "ast" <nomail@com.invalid> |
|---|---|
| Date | 2016-03-24 11:54 +0100 |
| Message-ID | <56f3c759$0$27835$426a74cc@news.free.fr> |
| In reply to | #105597 |
"David Palao" <dpalao.python@gmail.com> a écrit dans le message de news:mailman.86.1458816553.2244.python-list@python.org... > Hi, > Use "eval": > s = "(1, 2, 3, 4)" > t = eval(s) > > Best > Thank you
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-03-25 00:53 +1100 |
| Message-ID | <56f3f16f$0$1595$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #105597 |
On Thu, 24 Mar 2016 09:49 pm, David Palao wrote: > Hi, > Use "eval": > s = "(1, 2, 3, 4)" > t = eval(s) Don't use eval unless you absolutely, categorically, 100% trust the source of the string. Otherwise, you are letting the person who provided the string run any code they like on your computer. You want malware? That's how you get malware. -- Steven
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2016-03-24 15:05 +0000 |
| Message-ID | <nd0vnq$jik$2@reader1.panix.com> |
| In reply to | #105610 |
On 2016-03-24, Steven D'Aprano <steve@pearwood.info> wrote: > On Thu, 24 Mar 2016 09:49 pm, David Palao wrote: > >> Hi, >> Use "eval": >> s = "(1, 2, 3, 4)" >> t = eval(s) > > Don't use eval unless you absolutely, categorically, 100% trust the source > of the string. And then still don't use it. :) eval is only safe if you're passing it a literal string containing nothing but a literal constant expression -- in which case the eval is superflous. OK, I admit I've used it for quick hacks on occasion. But, I shouldn't have. -- Grant
[toc] | [prev] | [next] | [standalone]
| From | wxjmfauth@gmail.com |
|---|---|
| Date | 2016-03-24 10:11 -0700 |
| Message-ID | <ddfcf4f4-1ea0-435b-aac5-63e65b78d8d8@googlegroups.com> |
| In reply to | #105625 |
>>> s = "(1, 2, 3, 4)"
>>> a = tuple([int(e) for e in s.strip('()').split(',')])
>>> a
(1, 2, 3, 4)
>>>
[toc] | [prev] | [next] | [standalone]
| From | Matt Wheeler <m@funkyhat.org> |
|---|---|
| Date | 2016-03-24 10:57 +0000 |
| Message-ID | <mailman.87.1458817089.2244.python-list@python.org> |
| In reply to | #105596 |
>>> import ast >>> s = "(1, 2, 3, 4)" >>> t = ast.literal_eval(s) >>> t (1, 2, 3, 4) On 24 March 2016 at 10:39, ast <nomail@com.invalid> wrote: > Hi > > I have a string which contains a tupe, eg: > > s = "(1, 2, 3, 4)" > > and I want to recover the tuple in a variable t > > t = (1, 2, 3, 4) > > how would you do ? > > > -- > https://mail.python.org/mailman/listinfo/python-list -- Matt Wheeler http://funkyh.at
[toc] | [prev] | [next] | [standalone]
| From | Tim Chase <python.list@tim.thechases.com> |
|---|---|
| Date | 2016-03-24 05:58 -0500 |
| Message-ID | <mailman.88.1458817732.2244.python-list@python.org> |
| In reply to | #105596 |
On 2016-03-24 11:49, David Palao wrote: >> s = "(1, 2, 3, 4)" >> >> and I want to recover the tuple in a variable t >> >> t = (1, 2, 3, 4) >> >> how would you do ? > > Use "eval": > s = "(1, 2, 3, 4)" > t = eval(s) Using eval() has security implications. Use ast.literal_eval for safety instead: import ast s = "(1, 2, 3, 4)" t = ast.literal_eval(s) -tkc
[toc] | [prev] | [next] | [standalone]
| From | "Sven R. Kunze" <srkunze@mail.de> |
|---|---|
| Date | 2016-03-24 12:10 +0100 |
| Message-ID | <mailman.89.1458817825.2244.python-list@python.org> |
| In reply to | #105596 |
On 24.03.2016 11:57, Matt Wheeler wrote: >>>> import ast >>>> s = "(1, 2, 3, 4)" >>>> t = ast.literal_eval(s) >>>> t > (1, 2, 3, 4) I suppose that's the better solution in terms of safety.
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-03-24 22:13 +1100 |
| Message-ID | <56f3cbd2$0$1609$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #105596 |
On Thu, 24 Mar 2016 09:39 pm, ast wrote:
> Hi
>
> I have a string which contains a tupe, eg:
>
> s = "(1, 2, 3, 4)"
>
> and I want to recover the tuple in a variable t
>
> t = (1, 2, 3, 4)
>
> how would you do ?
py> import ast
py> ast.literal_eval("(1, 2, 3, 4)")
(1, 2, 3, 4)
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | Matt Wheeler <m@funkyhat.org> |
|---|---|
| Date | 2016-03-24 13:22 +0000 |
| Message-ID | <mailman.92.1458825746.2244.python-list@python.org> |
| In reply to | #105596 |
On Thu, 24 Mar 2016 11:10 Sven R. Kunze, <srkunze@mail.de> wrote: > On 24.03.2016 11:57, Matt Wheeler wrote: > >>>> import ast > >>>> s = "(1, 2, 3, 4)" > >>>> t = ast.literal_eval(s) > >>>> t > > (1, 2, 3, 4) > > I suppose that's the better solution in terms of safety. > It has the added advantage that the enquirer gets to import a module that shares their name ;) >
[toc] | [prev] | [next] | [standalone]
| From | "ast" <nomail@com.invalid> |
|---|---|
| Date | 2016-03-28 17:34 +0200 |
| Message-ID | <56f94f18$0$3314$426a34cc@news.free.fr> |
| In reply to | #105607 |
"Matt Wheeler" <m@funkyhat.org> a écrit dans le message de news:mailman.92.1458825746.2244.python-list@python.org... > On Thu, 24 Mar 2016 11:10 Sven R. Kunze, <srkunze@mail.de> wrote: > >> On 24.03.2016 11:57, Matt Wheeler wrote: >> >>>> import ast >> >>>> s = "(1, 2, 3, 4)" >> >>>> t = ast.literal_eval(s) >> >>>> t >> > (1, 2, 3, 4) >> >> I suppose that's the better solution in terms of safety. >> > > It has the added advantage that the enquirer gets to import a module that > shares their name ;) I had a look at that "ast" module doc, but I must admit that I didn't understood a lot of things.
[toc] | [prev] | [next] | [standalone]
| From | "Sven R. Kunze" <srkunze@mail.de> |
|---|---|
| Date | 2016-03-29 12:23 +0200 |
| Message-ID | <mailman.144.1459247027.28225.python-list@python.org> |
| In reply to | #105904 |
On 28.03.2016 17:34, ast wrote: > > "Matt Wheeler" <m@funkyhat.org> a écrit dans le message de > news:mailman.92.1458825746.2244.python-list@python.org... >> On Thu, 24 Mar 2016 11:10 Sven R. Kunze, <srkunze@mail.de> wrote: >> >>> On 24.03.2016 11:57, Matt Wheeler wrote: >>> >>>> import ast >>> >>>> s = "(1, 2, 3, 4)" >>> >>>> t = ast.literal_eval(s) >>> >>>> t >>> > (1, 2, 3, 4) >>> >>> I suppose that's the better solution in terms of safety. >>> >> >> It has the added advantage that the enquirer gets to import a module >> that >> shares their name ;) > > > I had a look at that "ast" module doc, but I must admit that > I didn't understood a lot of things. If there were a module "srkunze", I think, I would be equally surprised. ;) Best, Sven
[toc] | [prev] | [next] | [standalone]
| From | "Sven R. Kunze" <srkunze@mail.de> |
|---|---|
| Date | 2016-03-24 18:47 +0100 |
| Message-ID | <mailman.104.1458841672.2244.python-list@python.org> |
| In reply to | #105596 |
On 24.03.2016 14:22, Matt Wheeler wrote: > On Thu, 24 Mar 2016 11:10 Sven R. Kunze, <srkunze@mail.de> wrote: > >> On 24.03.2016 11:57, Matt Wheeler wrote: >>>>>> import ast >>>>>> s = "(1, 2, 3, 4)" >>>>>> t = ast.literal_eval(s) >>>>>> t >>> (1, 2, 3, 4) >> I suppose that's the better solution in terms of safety. >> > It has the added advantage that the enquirer gets to import a module that > shares their name ;) One shouldn't underestimate this. ;-)
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web