无法将 file.readline() 行与字符串进行比较 [英] Can't compare file.readline() line with a string
本文介绍了无法将 file.readline() 行与字符串进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图将我的 file.readline
中的一行与一个字符串进行比较,但这不起作用.我正在阅读一个 html 文件,我需要解析该文件以将该部分发送到字典,但是当我使用 if 将其中一行与字符串进行比较时,当它们匹配时它不会返回 True.
im trying to compare a line from my file.readline
with a string but this ain't working. I'm reading an html file which i need to parse to send the section to a dictionary but when i use an if to compare one of the lines with a string it doesn't return True when they match.
代码如下:
lines = f.readlines()
for line in lines:
if (line == '<TEXT>'):
while (line != '</TEXT>'):
body += line + ' '
content['text' + str(i)] = body
i += 1
body = ''
推荐答案
我认为这是因为它在字符串中读取换行符 try:
I think this is because its reading new line character in the string try:
for line in f:
line = line.rstrip()
if (line == '<TEXT>'):
这篇关于无法将 file.readline() 行与字符串进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文