python - Whether eval() better than self-analysis? -


here situation, have string follows

'a':1 'b':2 'c':3 

i want turn dict, have 2 options:

  1. split string ' ' , ':' put pairs dict.

  2. replace ' ' ',', append '{', , '}' string , use eval() dict.

so question 1 faster?

i this:

import ast result = ast.literal_eval(''.join(["{", s.replace(" ", ", "), "}"])) 

you can (although difference may negligible):

import ast result = ast.literal_eval("{" + s.replace(" ", ", ") + "}") 

it's better use ast.literal_eval it's safer purpose using eval().


Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -