python - 从文本文件中检索一个变量时比较 2 个变量

标签 python linecache

我正在尝试编写代码,将变量b与使用linecache.getline从文本文件检索的值进行比较

问题是它永远不会打印我们的“ITS WORKING”,因为这些值永远不会匹配,即使它们匹配:-(

文本文件:在文本文件中只有一个字符及其“a”

这是代码:

import linecache

b="a" 

a=linecache.getline("TextFile.txt",1)


if a==b:
    print("ITS WORKING")

最佳答案

根据documentation , linecache.getline 将包含尾随换行符,这就是您的匹配不起作用的原因。

关于python - 从文本文件中检索一个变量时比较 2 个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42909263/

相关文章:

python - 只有在成功创建其他对象时才创建对象

python - 用python粘贴没有添加黑色背景的图像

python - matplotlib bar3d 裁剪问题

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

ruby-on-rails - 安装 linecache ERROR : Failed to build gem native extension on 时在 Windows 机器上收到错误

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

python - 错误 : "http_archive" is not defined when starting the local Bazel server in the TensorFlow Installation

python - 并行线程 python GIL 与 Java

ruby-on-rails-3 - Ruby on Rails - 尝试 ' bundle install ' 时出现线缓存错误

ruby-on-rails - 为什么bundle install会尝试安装linecache19?