-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_parser.config
59 lines (53 loc) · 2.02 KB
/
csv_parser.config
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
/**
* This csv_parser namespace is used to load a CSV file with a header.
* The information is loaded as a list of Maps, with each line in the CSV corresponding to a Map.
*/
csv_parser {
/**
* Using the header parts, find the indices of each field requested.
* @throws IllegalArgumentException when a field is missing from the header.
*/
get_field_indices = { String header, List fields ->
def header_parts = header.split(',') as List
def field_indices = [:]
fields.each { field ->
field_indices[field] = header_parts.indexOf(field)
if (field_indices[field] == -1) {
throw new IllegalArgumentException("Failed to find field: ${field} in header of CSV.")
}
}
return field_indices
}
/**
* Split each line and parse contents into Map.
* @throws IllegalArgumentException when a field is missing from a single line.
*/
parse_line = { String line, Map field_indices, Boolean allow_empty ->
def line_parts = line.split(',') as List
def line_fields = [:]
field_indices.each { field, index ->
line_fields[field] = line_parts[index]
if (allow_empty) {
return
}
if (!line_fields[field]) {
throw new IllegalArgumentException("Field: ${field} not found for line: ${line}")
}
}
return line_fields
}
/**
* Main parsing function for calling. Returns parsed data as a list of Maps.
*/
parse_csv = { String csv_file, List fields, Boolean allow_empty=false ->
def reader = new BufferedReader(new FileReader(csv_file))
def header = reader.readLine()
def field_indices = csv_parser.get_field_indices(header, fields)
def parsed_lines = []
def line = ''
while ( (line = reader.readLine()) != null ) {
parsed_lines.add(csv_parser.parse_line(line, field_indices, allow_empty))
}
return parsed_lines
}
}