python - How to organise columnar data into a network table -


i have tab delimited file text file 2 columns. need find way prints values “hit” each other 1 line.

for example, input looks this:

a   b   c   d b   c b   d c   d b   e d   e b   f c   f f   g f   h h   k   l 

my desired output should this:

a   b   c   d b   d   e b   c   f f   g   h h   k   l 

my actual data file larger if makes difference. prefer in unix or python possible.

can help?

thanks in advance!

there's no way put input file .csv? easier parse delimiters.

if wouldn't posible, try next example:

from itertools import groupby operator import itemgetter open('example.txt','rb') txtfile:     cleaned = []     #store file information in list of lists     line in txtfile.readlines():         cleaned.append(line.split())     #group py first element of nested list     elt, items in groupby(cleaned, itemgetter(0)):         row = [elt]         item in items:             row.append(item[1])         print row 

hope helps you.

solution using .csv file:

from itertools import groupby  operator import itemgetter  import csv  open('example.csv','rb') csvfile:      reader = csv.reader(csvfile, delimiter='\t')      row in reader:           cleaned.append(row) #group py first element of nested list      elt, items in groupby(cleaned, itemgetter(0)):          row = [elt]          item in items:              row.append(item[1])          print row 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -