0

I am writing netcdf files and I was wondering if it is possible to import the global attributes straight from the csv file metadata instead of typing them in manually.

This is the conventional way how I used to do it

f.long_name = 'longitude of measurements'

f.standard_name = 'longitude'

f.units = 'degrees_east'

now I would like to skip this manual typing and just insert all the information at once from the csv file.

Somebody has an idea or experience with that? Or does it have to be typed in maunally?

Thanks in advance!

  • Does this [post](https://stackoverflow.com/questions/67971367/creating-netcdf-files-in-python-from-csv-data) help with your question? – user11717481 Feb 23 '22 at 13:17

1 Answers1

0

I don't know how you are loading CSV and what are those metadata that you mentioned, but if you have some pairs of keys and values and you wish to treat these keys as variables, there's the answer. there are several ways actually. for example, you can use globals() or locals() function to access a dict type containing variables and use a for loop or anything like that to change these variables.

But a more suggested way is to use a class like this

class Attrs:
    def __init__(self, dict):
        self._dict = dict
    
    def __getattr__(self, key):
        return self._dict[key]

and initialize it with the loaded variables, there you can access the variable using the desired syntax. like this

f = Attrs({'a': 1})
f.a # 1
Odeaxcsh
  • 133
  • 7