-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path15_csvExample.py
More file actions
executable file
·40 lines (28 loc) · 1.02 KB
/
15_csvExample.py
File metadata and controls
executable file
·40 lines (28 loc) · 1.02 KB
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
#!/usr/bin/env python3
"""
Example using the csv library
https://docs.python.org/3/library/csv.html
https://docs.python.org/3/library/os.html
Jens Dede, 2019, jd@comnets.uni-bremen.de
"""
import csv
import os
# Read data from csv file
# Join the paths in an OS independent way (/ vs \)
csvfile = os.path.join("files", "noisy-dataA.csv") # Works for Windows and Linux
with open(csvfile, "r") as f: # Open file
csvReader = csv.reader(f) # Treat file as csv file
for row in csvReader: # Iterate over all rows
print(row) # Print each row completely
print(row[0]) # Print only first entry (i.e. the time column)
# Write data to csv file
with open("output.csv", "w") as f:
csvWriter = csv.writer(f)
csvWriter.writerow(["a","b","c"])
csvWriter.writerow(["d","e","f"])
csvWriter.writerow([1,2,3])
print("Output written to \"output.csv\"")
# What to try out
#################
# - Open the output.csv in an text editor and as a spreadsheet. Do you see the
# connection?