[Tutor] File handling Tab separated files

Steven D'Aprano steve at pearwood.info
Thu Apr 19 09:37:17 EDT 2018


On Thu, Apr 19, 2018 at 10:45:07AM +0200, 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.

You should probably look at using the csv module. It can handle 
tab-separated files too.

https://docs.python.org/3/library/csv.html

https://docs.python.org/2/library/csv.html


> 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))

You have two errors here. Firstly, you should not call a.__init__ 
directly. You never call methods with two leading and trailing 
underscores directly: always let Python call them for you.

Instead, you call:

a = BioGRIDReader(filename)

Your second error is the file name. You write:

    test_biogrid.txt

but to Python, that looks like a variable called test_biogrid, which 
does not exist. That is why you get the error:

> Here's what the terminal says:
> Traceback (most recent call last):
>   File "./BioGRIDReader.py", line 23, in <module>
>     print (a.__init__(test_biogrid.txt))
> NameError: name 'test_biogrid' is not defined

The solution is to quote the name of the file, so Python treats it as a 
string:

a = BioGRIDReader("test_biogrid.txt")

By the way, THANK YOU for quoting the whole error message! That is much 
better than people who just say "it doesn't work" and leave us to guess 
what happens.




-- 
Steve


More information about the Tutor mailing list