Skip to content

Commit b25436b

Browse files
authored
Merge pull request #30 from simranmattu14/pg_updates
Added get_value method to easily get values from csv files
2 parents 36062cd + d0b2aab commit b25436b

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

woudc_extcsv/__init__.py

+33-1
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,6 @@ def init_table(self, table_name, fields, line_num):
382382
:param line_num: line number of the table's header (its name)
383383
:returns: final name for the new table
384384
"""
385-
386385
if table_name not in self._table_count:
387386
self._table_count[table_name] = 1
388387
else:
@@ -402,6 +401,39 @@ def init_table(self, table_name, fields, line_num):
402401

403402
return table_name
404403

404+
def get_value(self, table, field=None, index=None):
405+
"""
406+
Get value from table field.
407+
408+
:param table: table name
409+
:param field: field name
410+
:returns: value in the extcsv file.
411+
"""
412+
if table not in self.extcsv:
413+
raise KeyError(f"Table '{table}' not found in extcsv.")
414+
415+
if field is not None and field not in self.extcsv[table]:
416+
raise KeyError(f"Field '{field}' not found in table '{table}'.")
417+
418+
headers = [
419+
'CONTENT', 'DATA_GENERATION', 'PLATFORM',
420+
'INSTRUMENT', 'LOCATION', 'TIMESTAMP'
421+
]
422+
423+
if index == 1:
424+
return self.extcsv[table][field][0]
425+
426+
if table in headers and field is None:
427+
data = self.extcsv[table]
428+
for field in list(data.keys()):
429+
if field != 'comments' and isinstance(data[field], list):
430+
data[field] = data[field][0]
431+
return data
432+
elif table not in headers and field is None:
433+
return self.extcsv[table]
434+
435+
return self.extcsv[table][field]
436+
405437
def add_field_to_table(self, table_name, fields, index=1):
406438
"""
407439
Record an empty column(s) in Extended CSV table named <table_name>

0 commit comments

Comments
 (0)