How to remove "" from starting of a string if provided by the user

Matt Wheeler m at funkyhat.org
Wed Aug 12 21:38:48 EDT 2020


On Tue, 11 Aug 2020, 02:20 Ganesh Pal, <ganesh1pal at gmail.com> wrote:

> The possible value of stat['server2'] can be either (a)
> "'/fileno_100.txt'" or (b) '/fileno_100.txt' .
>
> How do I check if it the value was  (a) i.e string started and ended
> with a quote , so that I can use ast.literal_eval()
>

BAFP

>
def maybe_unquote(string):
    try:
        return ast.literal_eval(string)
    except ValueError:
        return string


More information about the Python-list mailing list