我如何让python将这两行识别为一行,因为它们是一句话。我正在从一个文本文件中读取这些内容。所以,如果我想检查单词是否在句子中,如果是的话,应该打印这两行,因为它们是一个句子。但是我的代码只打印第一行。
//! hello ID x86357 this is python programming language it's a very nice
//! programming language. 代码
with open("test_file.txt", "r") as csvfile:
for row in csvfile:
if 'ID' in row:
print(row)预期代码
//! hello this is python programming language it's a very nice
//! programming language. 实际结果
//! hello this is python programming language it's a very nice 发布于 2019-01-05 02:56:55
您必须指定读取文件的方式。这里使用的是"new line“默认分隔符。查看Build-in Functions并查看open()。
"...When正在读取来自流的输入,如果newline为None,则启用通用换行符模式。输入中的行可以以'\n‘、'\r’或‘\r\n’结尾。
您必须在流中指定分隔符,才能按您希望的方式进行读取。
发布于 2019-01-05 02:55:12
你可以使用像csv阅读器这样的东西。
import csv
with open('test_file.txt', 'r', newline='') as f:
reader = csv.reader(f)
for row in reader:
print(row[0])https://stackoverflow.com/questions/54044441
复制相似问题