galaxy9064
galaxy9064

Reputation: 9

Read GB2312 encoding file with linecache.getline

I need to read specified lines in a GB2312 encoding file with given line numbers. Many people mentioned linecache.getline(file_path,line_no) is the right answer. But it raised UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd6 in position 72: invalid continuation byte when I tried to read the GB2312 file, while works with utf-8 file.

However, in Python docs for linecache.getline() I didn't find any parameter that specifies encoding method.

So I want to know is linecache.getline() capable of reading non-utf-8 file? If yes, how?

Here is a minimal reproducible example.

import linecache
print(linecache.getline("test.txt",1))

Store the following text with Chinese characters as test.txt. Then use vs-code or notpad++ to covert it to GB2312 encoding.

First line 第一行
Second line 第二行

Upvotes: 0

Views: 355

Answers (0)

Related Questions