IMP  2.0.0
The Integrative Modeling Platform
csv_related.py
1 
2 import csv
3 
4 def is_comment(list_values, comment_char = "#"):
5  if(len(list_values) == 0 or list_values[0][0] == comment_char):
6  return True
7  return False
8 
9 def read_csv(fn, delimiter = "|", comment_char = "#", max_number=False):
10  """
11  Simple reader of csv files that disregards lines with comments
12  """
13  f = open(fn, "r")
14  reader = csv.reader(f, delimiter=delimiter)
15  if(max_number):
16  i = 1
17  rows = []
18  for r in reader:
19  if(i>max_number):
20  break
21  rows.append(r)
22  i+=1
23  else:
24  rows = [d for d in reader if(not is_comment(d, comment_char))]
25  f.close()
26  return rows
27 
28 
29 def read_csv_keyword(fn_or_f,keyword, delimiter = "|",
30  comment_char = "#"):
31  """
32  Reader of csv files that only recovers lines starting with a keyword
33 
34  """
35  if(type(fn_or_f) == str):
36  f = open(fn_or_f, "r")
37  elif(type(fn_or_f) == file):
38  f = fn_or_f
39 
40  reader = csv.reader(f, delimiter=delimiter)
41  rows = []
42  for row in reader:
43  if(not is_comment(row) and row[0] == keyword):
44  rows.append(row)
45 
46 
47  if(type(fn_or_f) == str):
48  f.close()
49 
50  return rows