watex.utils.funcutils.parse_csv#

watex.utils.funcutils.parse_csv(csv_fn=None, data=None, todo='reader', fieldnames=None, savepath=None, header=False, verbose=0, **csvkws)[source]#

Parse comma separated file or collect data from CSV.

Parameters:
  • csv_fn – csv filename,or output CSV name if data is given and todo is set to write|dictwriter.Otherwise the CSV output filename should be the c.data or the given variable name.

  • data – Sequence Data in Python obj to write.

  • todo – Action to perform with JSON: - reader|DictReader: Load data from the JSON file - writer|DictWriter: Write data from the Python object and create a CSV file

  • savepath – If default should save the csv_fn If path does not exist, should save to the <’_savecsv_’> default path.

  • fieldnames

    is a sequence of keys that identify the order in which values in the dictionary passed to the writerow()

    method are written csv_fn file.

  • savepath – If default should save the csv_fn If path does not exist, should save to the <’_savecsv_’> default path .

  • verbose – int, control the verbosity. Output messages

  • csvkws – additional keywords csv class arguments

https://stackoverflow.com/questions/10373247/how-do-i-write-a-python-dictionary-to-a-csv-file

Example:
>>> import watex.utils.funcutils as FU
>>> PATH = 'data/model'
>>> k_ =['model', 'iter', 'mesh', 'data']
>>> try :
    INVERS_KWS = {
        s +'_fn':os.path.join(PATH, file)
        for file in os.listdir(PATH)
                  for s in k_ if file.lower().find(s)>=0
                  }
except :
    INVERS=dict()
>>> TRES=[10, 66,  70, 100, 1000, 3000]# 7000]
>>> LNS =['river water','fracture zone', 'MWG', 'LWG',
      'granite', 'igneous rocks', 'basement rocks']
>>> geo_kws ={'oc2d': INVERS_KWS,
              'TRES':TRES, 'LN':LNS}
>>> # write data and save to  'csvtest.csv' file
>>> # here the `data` is a sequence of dictionary geo_kws
>>> FU.parse_csv(csv_fn = 'csvtest.csv',data = [geo_kws],
                 fieldnames = geo_kws.keys(),todo= 'dictwriter',
                 savepath = 'data/saveCSV')
# collect csv data from the 'csvtest.csv' file
>>> FU.parse_csv(csv_fn ='data/saveCSV/csvtest.csv',
                 todo='dictreader',fieldnames = geo_kws.keys()
                 )