编辑:我犯了一个简单的错误 'testing 1,2,3+\n' 应该是 'testing 1,2,3'+'\n' 或 'testing 1,2,3\n'
我正在尝试有条件地从文件中读取文本。当我将 my_file.readline() 与 some_file.readline() 进行比较时,我无法弄清楚将返回 True 的条件
my_file.readline() == 'string literal'总是返回 false。
with open('text.txt','w') as my_file:
my_file.write('testing 1,2,3'+'\n'+'filter me'+'\n') #writing arbitrary text to my file
with open('text.txt','r') as my_file:
str_from_file = my_file.readline() # first line should be 'testing 1,2,3'
print str_from_file == 'testing 1,2,3+\n' #both print False
print str_from_file == 'testing 1,2,3' #what string literal would print true?
print str_from_file
显然,我是 python 和编码的大块头。这是我使用 Python 的第五天。