Python 行缓存不工作

标签 python python-3.x linecache

在Python中,我遇到一个问题,每当我使用linecache模块中的getline()函数时,它根本不起作用。假设这是我在名为 hi.txt 的文本文档中得到的内容:

Hi

并说这是我在同一文件夹/目录中的 python 程序上的内容:

import linecache

print (linecache.getline("hi.txt", 0))

它不会打印任何内容,只是一些空行。

最佳答案

linecache.getline 从 1 开始。

print (linecache.getline("hi.txt", 1))

如您所愿

>>> help(linecache.getline)
> getline(filename, lineno, module_globals=None)

按照惯例,lineno 在任何文本编辑器中都从 1 开始。

关于Python 行缓存不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41698486/

相关文章:

python-3.x - 在具有相同 ID 的行末尾添加 Pandas 值

Python 修改列表列表

ruby - "bundle install"错误

python - Flask - 不允许的方法

python - 读取在 C 中使用 Python struct 编写的二进制数据

python - pandas 移动列名称并用 NAN 填充最后一列

Python - 打开文本文件进行编辑,然后使用 fileinput.input() 对其进行修改

Python,括号对打印语句中 bool 值的影响

python - 用文本文件中的相应行替换列表列表中的数字

python - 从特定文本行读取文件