0

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 第二行
  • The actual code in the module seems rather special-purpose, and was designed to read and cache lines of Python code. The `open()` calls etc simply don't specify an encoding. I'm guessing your best bet for the immediate term is to try to apply the hints for how to override the default behavior by adding a method to the global namespace (which sounds so hackish as to almost be offensive, but maybe I'm a wussy). Longer term, perhaps a bug report would be in order. – tripleee Jan 26 '22 at 16:49
  • 1
    The `linecache` module allows one to get any line from a *Python source file*. The default encoding for Python 3 source code is `UTF-8`. Please [edit] your question to provide a [mcve]. – JosefZ Jan 26 '22 at 17:27
  • The linked documentation says "_This function will never raise an exception — it will return '' on errors_" so ***something else*** must be raising the exception. Please (also) include the entire traceback in your question. – martineau Jan 26 '22 at 17:29
  • https://stackoverflow.com/questions/4999340/python-random-access-file has answers with other solutions which may be less painful for your use case. – tripleee Jan 26 '22 at 18:26
  • Thanks for adding the MCVE. This example doesn't exactly convince us that you need random access, though. I have a hunch that this may actually be an [XY Problem](https://en.wikipedia.org/wiki/XY_problem). What is the actual use case here? – tripleee Jan 27 '22 at 06:37

0 Answers0