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

MRAB python at mrabarnett.plus.com
Mon Aug 10 18:33:46 EDT 2020


On 2020-08-10 19:35, Ganesh Pal wrote:
> How to remove " from the starting and ending of a string , before
> comparison . Here is an example and my solution wtih eval ( I am advised
> not to use this one) , please suggest an alternative . I am on linux and
> python 2.7
> 
> g1 at X1:/tmp$ cat file2.py
> #!/usr/bin/python
> 
> # Case 1 - server2 file is "'/fileno_100.txt'"
> stat={}
> stat['server1']  = '/fileno_100.txt'
> stat['server2']  = "'/fileno_100.txt'"
> 
> if stat['server1'] == eval(stat['server2']):
>      print "OK"
> 
> # Case 2 - server2 file is '/fileno_100.txt'
> stat['server2']  = "'/fileno_100.txt'"
> 
> if stat['server1'] == eval(stat['server2']):
>      print "OK"
> 
> 
> # Case 3 - server2 file can be in (a) '/fileno_100.txt' or (b) :
> "'/fileno_100.txt'" format
> 
> g1 at X1:/tmp$ python file2.py
> OK
> OK
> 
You could strip off the quotes with the .strip method or use 
literal_eval from the ast module.


More information about the Python-list mailing list