Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
RoutingPP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
IPSL
LMD
InTro
RoutingPP
Commits
d0cbda0f
Commit
d0cbda0f
authored
1 month ago
by
POLCHER Jan
Browse files
Options
Downloads
Patches
Plain Diff
Improved function to read the CSV files.
parent
1514b4fb
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Locations.py
+49
-14
49 additions, 14 deletions
Locations.py
with
49 additions
and
14 deletions
Locations.py
+
49
−
14
View file @
d0cbda0f
...
...
@@ -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
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment