简体   繁体   中英

Python - Get empty key from yml file

I have a lot yaml file names that have similar structure but with different data. I need to parse out selective data, and put into a single csv (excel) file as three columns.

But i facing an issue with empty key, that always gives me an "KeyError: 'port'"

my yaml file example:

base:
   server: 10.100.80.47
   port: 3306
   namePrefix: well
   user: user1
   password: kjj&%$

 base:
   server: 10.100.80.48
   port: 
   namePrefix: done
   user: user2
   password: fhfh#$%

In the second block i have an empty "port", and my script is stuck on that point. I need that always that an empty key is found it doesn't write anything.

   from asyncio.windows_events import NULL
   from queue import Empty
   import yaml
   import csv
   import glob


   yaml_file_names = glob.glob('./*.yaml')

   rows_to_write = []

   for i, each_yaml_file in enumerate(yaml_file_names):
   print("Processing file {} of {} file name: {}".format(
    i+1, len(yaml_file_names),each_yaml_file))

   with open(each_yaml_file) as file: 
    data = yaml.safe_load(file)


   for v in data:
    if "port" in v == "": 
    data['base']['port'] = ""  



  rows_to_write.append([data['base']['server'],data['base']['port'],data['server']['host'],data['server']['contex']])


  with open('output_csv_file.csv', 'w', newline='') as out:
    csv_writer = csv.writer(out)
    csv_writer.writerow(["server","port","hostname", "contextPath"])
    csv_writer.writerows(rows_to_write)
    print("Output file output_csv_file.csv created")

输出

You are trying to access the key by index eg

data['base']['port']

But what you want is to access it with the get method like so:

data['base'].get('port')

This way if the key does not exists it return None as default, and you could even change the default value to whatever you want by passing it as the second parameter.

In PyYAML, an empty element is returned as None , not an empty string.

if data['base']['port'] is None: 
    data['base']['port'] = "" 

Your yaml file is invalid. In yaml file, whenever you have a key (like port: in your example) you must provide a value, you cannot leave it empty and go to the next line.

This is likely why you cannot parse the file as expected with the python yaml module. If you are the creator of those yaml file, you really need to put a key in the file like port: None if you don't want to provide a value for the port, or even better you just not provide any port key at all.

Then the other solutions should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM