Why is PyYAML spending so much time in just parsing a YAML File?

前端 未结 1 1903
萌比男神i
萌比男神i 2021-02-14 01:35

I am parsing a YAML file with around 6500 lines with this format:

foo1:
  bar1:
    blah: { name: \"john\", age: 123 }
  metadata: { whatever1: \"whatever\", wha         


        
1条回答
  •  臣服心动
    2021-02-14 02:20

    According to the docs you must use CLoader/CSafeLoader (and CDumper):

    import yaml
    try:
        from yaml import CLoader as Loader
    except ImportError:
        from yaml import Loader
    
    config_file = "test.yaml"
    
    stream = open(config_file, "r")
    sensors = yaml.load(stream, Loader=Loader)
    

    This gives me

    real    0m0.503s
    

    instead of

    real    0m2.714s
    

    0 讨论(0)
提交回复
热议问题