[Tutor] File handling Tab separated files

Niharika Jakhar niharika1883 at gmail.com
Thu Apr 19 12:50:25 EDT 2018


Hi again
I tried re-writing the code with all your advices(i assume to cover all of
them). I have extended the code a little bit to store the data in the form
of lists and am trying to access it.
I also changed the file name to BioGRID.txt

Here's what I wrote(Please ignore the identation, there was no such error,
it's just the e-mail thingy.):

import csv
class BioGRIDReader:
    def __init__(self, filename):
        with open('filename', 'rb') as f:
            self.reader = csv.reader(f, delimiter='\t')
            self.object_ = self.reader.split['\n']
            for row in range(len(object_)):
                for r in range(len(row)):
                    if (r[-1] == r[-2]):
                        return (r[2], r[3]) #returns pair of taxon ids






a = BioGRIDReader('BioGRID.txt')
print(a.object[:100])




here's what the compiler says:
Traceback (most recent call last):
  File "./BioGRIDReader.py", line 17, in <module>
    a = BioGRIDReader('BioGRID.txt')
  File "./BioGRIDReader.py", line 4, in __init__
    with open('filename', 'rb') as f:
IOError: [Errno 2] No such file or directory: 'filename'





I am extremely sorry if I have repeated a same mistake again, but I did
what I understood.
Thanks again for the links and I am not allowed to use different packages
and have to strictly use the standard python library. But thanks anyway, it
will be of great help in future. :)


On Thu, Apr 19, 2018 at 4:50 PM, Mats Wichmann <mats at wichmann.us> wrote:

> On 04/19/2018 07:57 AM, Wolfgang Maier wrote:
> > On 04/19/2018 10:45 AM, Niharika Jakhar wrote:
> >> Hi
> >> I want to store a file from BioGRID database (tab separated file, big
> >> data)
> >> into a data structure(I prefer lists, please let me know if another
> would
> >> be better) and I am trying to print the objects.
> >> Here’s my code:
> >> class BioGRIDReader:
> >>      def __init__(self, filename):
> >>              with open('filename', 'r') as file_:
> >>              read_data = f.read()
> >>              for i in file_ :
> >>                  read_data = (i.split('\t'))
> >>                  return (objects[:100])
> >>
> >> a = BioGRIDReader
> >> print (a.__init__(test_biogrid.txt))
> >>
> >
> > In addition to your immediate problem, which Steven explained already,
> > you will run into more issues with the posted code:
>
> In addition to this low level advice, let me observe that whenever the
> term "big data" is tossed into the discussion, you want to consider
> whether reading it all in to Python's memory into a "simple" data
> structure in one go is what you want to do.  You may want to look into
> the Pandas project (possibly after spending a little more time becoming
> comfortable with Python itself first):
>
> https://pandas.pydata.org/
>
> Pandas has its own file handling code (particularly, a read_csv
> function) which might end up being useful.
>
>
> Also quite by chance, I happen to know there's an existing project to
> interact with the BioGRID web service, have no idea if that would be a
> match for any of your needs.  A quick google to refind it:
>
> https://github.com/arvkevi/biogridpy
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list