|
a |
|
b/dataprocess/csvTools.py |
|
|
1 |
''' |
|
|
2 |
provided by LUNA16 |
|
|
3 |
evaluation script |
|
|
4 |
''' |
|
|
5 |
|
|
|
6 |
import csv |
|
|
7 |
import platform |
|
|
8 |
|
|
|
9 |
def writeTXT(filename, lines): |
|
|
10 |
with open(filename, 'w') as f: |
|
|
11 |
for line in lines: |
|
|
12 |
f.write(line+'\n') |
|
|
13 |
|
|
|
14 |
def writeCSV(filename, lines): |
|
|
15 |
with open(filename, "w", newline='') as f: |
|
|
16 |
csvwriter = csv.writer(f) |
|
|
17 |
csvwriter.writerows(lines) |
|
|
18 |
|
|
|
19 |
def readCSV(filename): |
|
|
20 |
lines = [] |
|
|
21 |
with open(filename, "r") as f: |
|
|
22 |
csvreader = csv.reader(f) |
|
|
23 |
for line in csvreader: |
|
|
24 |
lines.append(line) |
|
|
25 |
return lines |
|
|
26 |
|
|
|
27 |
def tryFloat(value): |
|
|
28 |
try: |
|
|
29 |
value = float(value) |
|
|
30 |
except: |
|
|
31 |
value = value |
|
|
32 |
|
|
|
33 |
return value |
|
|
34 |
|
|
|
35 |
def getColumn(lines, columnid, elementType=''): |
|
|
36 |
column = [] |
|
|
37 |
for line in lines: |
|
|
38 |
try: |
|
|
39 |
value = line[columnid] |
|
|
40 |
except: |
|
|
41 |
continue |
|
|
42 |
|
|
|
43 |
if elementType == 'float': |
|
|
44 |
value = tryFloat(value) |
|
|
45 |
|
|
|
46 |
column.append(value) |
|
|
47 |
return column |