开发者

error: integer required readlines()

I'm trying to open a CSV document using readlines() in Python.

开发者_运维知识库

The document contains a list of words, but I'm getting the error that an integer is required. Here's what I wrote and the error message I received:

>>> f = open('mike_only_genes.csv')
>>> a = f.readlines('mike_only_genes.csv')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: an integer is required


you are calling it wrong:

f.readlines('mike_only_genes.csv')

should be:

f.readlines()


Because readlines can be executed alone or with an integer, if you want to read all lines, just use readlines().

with open('mike_only_genes.csv') as f:
    lines = f.readlines()


Read more about readlines method: if sizehint parameter is specified than 'sizehint bytes (possibly after rounding up to an internal buffer size) are read', but you have specified a string that is why it throwing an exception about integer value:

f = open('mike_only_genes.csv') 
a = f.readlines() 

OR use:

with open('mike_only_genes.csv') as f:
    for line in f:
        # do something

OR:

for line in open('mike_only_genes.csv'):
    # do something
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜