0

I am curently extracting values in a csv file to incorporate them in yaml files.

The issue is that in the ~6000 lines in my csv files, a few cells are empty and I would like to skip those cells and continue my loop, which is like:

import csv
import ruamel.yaml
from csv import reader
from ruamel.yaml import YAML

yaml = ruamel.yaml.YAML()
yaml.preserve_quotes = True

with open('config.yaml') as yml:
    doc = yaml.load(yml)

with open('params.csv') as f:
    next(f)
    for i, data in enumerate(reader(f)):
        doc['components']['star']['init'][0]['values']['logg'] = float(data[4])
        filename = data[1].split(".")[0] 
        with open(f'{filename}.yaml', 'w') as out:
            yaml.dump(doc, out)

With the csv file looking like:

Starid   filename                                   Teff           TeffErr        logg
1        ngc6397id000000002jd2456871p6250f000.fits  6304.19055182  97.72410249    4.21614564
2        pgc6397id000000002jd2456871p6250f000.fits  7304.19055182  97.72410249    
3        qgc6397id000000002jd2456871p6250f000.fits  8304.19055182  97.72410249    5.21614564

I have tried the solution from Python: How to check if cell in CSV file is empty? But I think I do it wrong, I can not see where to integrate the if not row[0] loop at the right place, any idea ? Thanks !

Geo573
  • 142
  • 1
  • 10

1 Answers1

0

If I understand your problem correctly, you can just insert a check of the value like this:

with open('params.csv') as f:
    next(f)
    for i, data in enumerate(reader(f)):
        if data[4] != '':
            doc['components']['star']['init'][0]['values']['logg'] = float(data[4])
            filename = data[1].split(".")[0] 
            with open(f'{filename}.yaml', 'w') as out:
                yaml.dump(doc, out)
Steinar Lima
  • 7,644
  • 2
  • 39
  • 40