Skip to content
Snippets Groups Projects
Commit d0cbda0f authored by POLCHER Jan's avatar POLCHER Jan :bicyclist_tone4:
Browse files

Improved function to read the CSV files.

parent 1514b4fb
No related branches found
No related tags found
No related merge requests found
......@@ -43,28 +43,63 @@ else :
points_path = config.get("OverAll","Points_path",fallback=mpath)
#
def readcsv(csvfile) :
def readcsv(f) :
'''
Object : Read a CSV file separated by comas "," and put the result in a dictionary
where the keys are the names in the first line of the file.
Input :
csvfile : A csv file with the first line the column names and comas to separate.
f : A csv file with the first line the column names and comas to separate.
Output :
A dictionary with the key being the name of the columns and the list of values.
'''
with open(csvfile, newline='') as csvfile:
cf = csv.reader(csvfile, delimiter=',', quotechar='|')
o={}
for row in cf:
if len(o) == 0 :
lab=[]
for l in row :
o[l]=[]
lab.append(l)
with open(f) as csvfile:
reader = csv.DictReader(csvfile, delimiter=",")
x={}
for row in reader:
for k in row.keys() :
if k not in x.keys() :
x[k]=[row[k]]
else :
x[k].append(row[k])
#
# Detect type in each list and convert appropriatly
#
ctype={}
for k in x.keys() :
if k.find("lon") >= 0 or k.find("lat") >= 0 or k.find("Err") >= 0 :
ctype[k]="float"
else :
xt=[s.replace("-","",1).replace(".", "", 1).strip() for s in x[k]]
xx=[s.replace("-","",1).strip() for s in x[k]]
if "".join(xt).isdigit() :
i="".join(xx).count(".")
if i > 0 :
ctype[k]="float"
else :
ctype[k]="int"
else :
for i,e in enumerate(row) :
l=lab[i]
o[l].append(e)
ctype[k]="str"
#
# Convert for final output dictionary
#
o={}
for k in x.keys() :
if ctype[k].find("float") >= 0 :
o[k]=[]
for s in x[k] :
if len(s) > 0 :
o[k].append(float(s))
else :
o[k].append(np.nan)
elif ctype[k].find("int") >= 0 :
o[k]=[]
for s in x[k] :
if len(s) > 0 :
o[k].append(int(s))
else :
o[k].append(-9999)
else :
o[k]=x[k]
return o
#
class Locations :
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment