Skip to content
Snippets Groups Projects
Commit 73b28975 authored by Michele Maris's avatar Michele Maris
Browse files

u

parent 6d35c8df
No related branches found
No related tags found
No related merge requests found
from collections import OrderedDict
import pandas
class ya_extended_csv :
""" Yet Another extended csv manages
extended csv is csv with:
......@@ -33,17 +38,14 @@ declares the python interpreter
the separator can be a single character or a sequence
"""
from collections import OrderedDict
import pandas
class ya_extended_csv :
@property
def header(self) :
"""returns the header"""
return self._header
#
@property
def skipUndef(self) :
"""get/set the skipUndef flag"""
return self._skipUndef
@skipUndef.setter
def skipUndef(self,this) :
......@@ -51,6 +53,7 @@ class ya_extended_csv :
#
@property
def sep(self) :
"""get/set the separator"""
return self._sep
#
@sep.setter
......@@ -59,11 +62,13 @@ class ya_extended_csv :
#
@property
def reserved_tags(self) :
"""list of tags reserved (can not be used by the user)"""
return ['creator','created','creator_version','header_version','sep','tag_comment','tag_variable','filename','author'
,'!__index_label','!__sep__','!__tag_comment__','!__tag_variable__','!__assign_operator__','!__index_label__']
#
@property
def doNotLoadTags(self) :
"""list of tags not to be loaded"""
return ['BEGIN','END','!__index_label','!__sep__','!__tag_comment__','!__tag_variable__','!__assign_operator__','!__index_label__']
#
def __init__(self,sep=',',tag_comment='#',tag_variable='#!',assign_operator='=',skipUndef=True,undefValue=None,index_column=0) :
......@@ -111,21 +116,32 @@ syntax elements can be changed modifiing parameters
if not self._skipUndef :
self._header[a]=self._undefValue
#
@property
def hdr_to_str(self) :
"header converted to string, fields separed by '\n'."
if len(self._header.keys())==0 : return ""
out=[k+'='+str(self._header[k]) for k in self._header.keys()]
return '\n'.join(out)
#
def get_data(self,sep) :
"get the data"
engine='c' if len(sep)==1 else 'python'
if self._verbose : print(engine,sep)
self._csv=pandas.read_csv(self._fname,sep=sep,comment=self._tag_comment,engine=engine,index_col=self._index_column)
#
def set_pandas_attrs(self) :
"set the .attrs dictionary of pandas table"
if len(self._header.keys())>0 :
for k in self._header.keys() :
if k not in self.doNotLoadTags :
self._csv.attrs[k]=self._header[k]
#
def to_pandas(self) :
"to pandas table"
return self._csv
#
def to_dict(self) :
"to dictionary"
out=OrderedDict()
for k in self._csv.keys() :
out[k]=self._csv[k].values
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment