FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
random_python_scripts/csv_test.py at master · 702-Tony/random_python_scripts · GitHub
702-Tony
random_python_scripts
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
random_python_scripts
/
csv_test.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
76 lines (74 loc) · 2.44 KB
Breadcrumbs
random_python_scripts
/
csv_test.py
Copy path
File metadata and controls
76 lines (74 loc) · 2.44 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# csv_test
# i plan on trying to open a csv and iterate through the row like a list
import
csv
# instantiate helper vars
n_line_yr
=
-
1
s_line_yr
=
-
1
output_n
=
[]
output_s
=
[]
# builds a clean list with 14 empty spots
def
build_list
():
new_list
=
[]
for
i
in
range
(
14
):
new_list
.
append
(
''
)
return
new_list
# open old csv
with
open
(
'seaice_avg.csv'
,
newline
=
''
)
as
csvfile
:
csv_r
=
csv
.
reader
(
csvfile
,
delimiter
=
' '
,
quotechar
=
'|'
)
line_n
=
build_list
()
line_s
=
build_list
()
count
=
0
# iterate through the rows and store the data for each month in a list
# that will then be stored inside another list
for
row
in
csv_r
:
count
+=
1
rw_ls
=
row
[
0
].
split
(
','
)
year
=
rw_ls
[
0
]
if
count
>
1
:
mon
=
int
(
rw_ls
[
1
])
hemisphere
=
rw_ls
[
2
]
extent_avg
=
rw_ls
[
3
]
if
hemisphere
==
'north'
:
# new_year = new_line
if
n_line_yr
!=
year
:
# append finished line to output
output_n
.
append
(
line_n
)
# clear line
line_n
=
build_list
()
# set new line_yr to current
n_line_yr
=
year
# put your data together
line_n
[
0
]
=
n_line_yr
line_n
[
mon
]
=
extent_avg
line_n
[
13
]
=
hemisphere
elif
hemisphere
==
'south'
:
if
s_line_yr
!=
year
:
# append finished line to output
output_s
.
append
(
line_s
)
# clear line
line_s
=
build_list
()
# set new line_yr to current
s_line_yr
=
year
line_s
[
0
]
=
s_line_yr
line_s
[
mon
]
=
extent_avg
line_s
[
13
]
=
hemisphere
# append final lines to outputs
output_n
.
append
(
line_n
)
output_s
.
append
(
line_s
)
# output file
print
(
'csv successfully read'
)
with
open
(
'sea_ice_averages_transformed.csv'
,
'w'
,
newline
=
''
)
as
csvfile
:
csv_w
=
csv
.
writer
(
csvfile
,
delimiter
=
','
,
quotechar
=
'
\'
'
,
quoting
=
csv
.
QUOTE_MINIMAL
)
# removes first blank from both lists
output_n
.
pop
(
0
)
output_s
.
pop
(
0
)
# # adds hemisphere data from
# for i in output_n:
# i.append('north')
# for i in output_s:
# i.append('south')
header
=
[
'year'
,
'jan'
,
'feb'
,
'mar'
,
'apr'
,
'may'
,
'jun'
,
'jul'
,
'aug'
,
'sep'
,
'oct'
,
'nov'
,
'dec'
,
'hemisphere'
]
csv_w
.
writerow
(
header
)
csv_w
.
writerows
(
output_n
)
csv_w
.
writerows
(
output_s
)
print
(
'csv successfully written'
)
Back
|
FazBrowse Home
|
New Git URL