[Tutor] File handling Tab separated files
Niharika Jakhar
niharika1883 at gmail.com
Wed Apr 25 13:34:43 EDT 2018
hi again
when I #print (self.organismA) under the for x in self.results: , it
results in what it is supposed to be.
But when i print it in the below function, it gives some garbage value.
Kindly let me know what is wrong. :)
import functools
import csv
import time
start =time.time()
class BioGRIDReader:
def __init__(self, filename):
self.results = []
self.organisms = {}
i = 0
with open(filename) as f:
for line in csv.reader(f, delimiter = '\t'):
i += 1
if i>35:
self.results.append(line)
#print (self.results)
for x in self.results:
self.organismA = x[2]
self.organismB = x[3]
self.temp = (x[2],)
self.keys = self.temp
self.values = [x[:]]
self.organisms = dict(zip(self.keys, self.values))
#print (self.organismA)
#print (self.results[0:34]) #omitted region
def getMostAbundantTaxonIDs(self,n):
#print (self.organismA)
self.temp_ = 0
self.number_of_interactions = []
self.interaction_dict = {}
for x in self.organismA:
for value in self.organisms:
if (x in value):
self.temp_ += 1
self.number_of_interactions.append(self.temp_)
self.interaction_dict = dict(zip(self.organismA,
self.number_of_interactions))
a = BioGRIDReader("BIOGRID-ALL-3.4.159.tab.txt")
a.getMostAbundantTaxonIDs(5)
end = time.time()
#print(end - start)
Thanking you in advance
Best Regards
NIHARIKA
On Fri, Apr 20, 2018 at 11:06 AM, Alan Gauld <alan.gauld at yahoo.co.uk> wrote:
>
> Use Reply-All or Reply-List to include the mailing list in replies.
>
> On 20/04/18 09:10, Niharika Jakhar wrote:
> > Hi
> >
> > I want to store the data of file into a data structure which has 11
> > objects per line , something like this:
> > 2354 <tab> somethin2 <tab> 23nothing <tab> 23214.....
> >
> >
> > so I was trying to split the lines using \n and storer each line in a
> > list so I have a list of 11 objects, then I need to retrieve the last
> > two position,
>
> You are using the csv module so you don't need to split the lines, the
> csv reader has already done that for you. It generates a sequence of
> tuples, one per line.
>
> So you only need to do something like:
>
> results = []
> with open(filename) as f:
> for line in csv.reader(f, delimiter='\t'):
> if line[-1] == line[-2]:
> results.append(line[2],line[3])
>
> Let the library do the work.
>
> You can see what the reader is doing by inserting a print(line) call
> instead of the if statement. When using a module for the first time
> don't be afraid to use print to check the input/output values.
> Its better than guessing.
>
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
More information about the Tutor
mailing list