MCPcopy Create free account
hub / github.com/ActiveState/code / getColumns

Function getColumns

recipes/Python/577444_Get_columns_dattext/recipe-577444.py:1–38  ·  view source on GitHub ↗

Get columns of data from inFile. The order of the rows is respected :param inFile: column file separated by delim :param header: if True the first line will be considered a header line :returns: a tuple of 2 dicts (cols, indexToName). cols dict has keys that are headings i

(inFile, delim="\t", header=True)

Source from the content-addressed store, hash-verified

1def getColumns(inFile, delim="\t", header=True):
2 """
3 Get columns of data from inFile. The order of the rows is respected
4
5 :param inFile: column file separated by delim
6 :param header: if True the first line will be considered a header line
7 :returns: a tuple of 2 dicts (cols, indexToName). cols dict has keys that
8 are headings in the inFile, and values are a list of all the entries in that
9 column. indexToName dict maps column index to names that are used as keys in
10 the cols dict. The names are the same as the headings used in inFile. If
11 header is False, then column indices (starting from 0) are used for the
12 heading names (i.e. the keys in the cols dict)
13 """
14 cols = {}
15 indexToName = {}
16 for lineNum, line in enumerate(inFile):
17 if lineNum == 0:
18 headings = line.split(delim)
19 i = 0
20 for heading in headings:
21 heading = heading.strip()
22 if header:
23 cols[heading] = []
24 indexToName[i] = heading
25 else:
26 # in this case the heading is actually just a cell
27 cols[i] = [heading]
28 indexToName[i] = i
29 i += 1
30 else:
31 cells = line.split(delim)
32 i = 0
33 for cell in cells:
34 cell = cell.strip()
35 cols[indexToName[i]] += [cell]
36 i += 1
37
38 return cols, indexToName

Callers

nothing calls this directly

Calls 3

enumerateFunction · 0.50
splitMethod · 0.45
stripMethod · 0.45

Tested by

no test coverage detected