Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
462 views
in Technique[技术] by (71.8m points)

python - "an integer is required" when open()'ing a file as 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...