Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"an integer is required" when open()'ing a file as utf-8?

Tags:

python

utf-8

I have a file I'm trying to open up in python with the following line:

f = open("C:/data/lastfm-dataset-360k/test_data.tsv", "r", "utf-8")

Calling this gives me the error

TypeError: an integer is required

I deleted all other code besides that one line and am still getting the error. What have I done wrong and how can I open this correctly?

like image 723
Jim Avatar asked Apr 01 '12 23:04

Jim


3 Answers

From the documentation for open():

open(name[, mode[, buffering]])

[...]

The optional buffering argument specifies the file’s desired buffer size: 0 means unbuffered, 1 means line buffered, any other positive value means use a buffer of (approximately) that size. A negative buffering means to use the system default, which is usually line buffered for tty devices and fully buffered for other files. If omitted, the system default is used.

You appear to be trying to pass open() a string describing the file encoding as the third argument instead. Don't do that.

like image 198
Kristian Glass Avatar answered Nov 19 '22 14:11

Kristian Glass


You are using the wrong open.

>>> help(open)
Help on built-in function open in module __builtin__:

open(...)
    open(name[, mode[, buffering]]) -> file object

    Open a file using the file() type, returns a file object.  This is the
    preferred way to open a file.  See file.__doc__ for further information.

As you can see it expects the buffering parameter which is a integer.

What you probably want is codecs.open:

open(filename, mode='rb', encoding=None, errors='strict', buffering=1)
like image 35
Glider Avatar answered Nov 19 '22 13:11

Glider


From the help docs:

open(...)
    open(file, mode='r', buffering=-1, encoding=None,
         errors=None, newline=None, closefd=True) -> file object

you need encoding='utf-8'; python thinks you are passing in an argument for buffering.

like image 4
ninjagecko Avatar answered Nov 19 '22 14:11

ninjagecko