Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在搜索字符时,我删除了*.txt文件中多余的\n并将其打印出来。但我不知道';我不明白为什么它只打印这个_Python 3.x_Printing_Full Text Search - Fatal编程技术网

Python 3.x 在搜索字符时,我删除了*.txt文件中多余的\n并将其打印出来。但我不知道';我不明白为什么它只打印这个

Python 3.x 在搜索字符时,我删除了*.txt文件中多余的\n并将其打印出来。但我不知道';我不明白为什么它只打印这个,python-3.x,printing,full-text-search,Python 3.x,Printing,Full Text Search,如我在评论中所说,在19:19编辑 首先是文本文件: 我的代码是: xfile = open('mbox-short.txt') for line in xfile: # La prochaine ligne est pour enlever les espaces supperflus line = line.rstrip() if line.startswith('From:') : print(line) 输出如下: From: stephen.m

如我在评论中所说,在19:19编辑

首先是文本文件: 我的代码是:

xfile = open('mbox-short.txt')
for line in xfile:
    # La prochaine ligne est pour enlever les espaces supperflus
    line = line.rstrip()
    if line.startswith('From:') :
        print(line)
输出如下:

From: stephen.marquard@uct.ac.za
From: louis@media.berkeley.edu
From: zqian@umich.edu
From: rjlowe@iupui.edu
From: zqian@umich.edu
还有更多

我在这方面的要求。当我的程序找到我要找的东西时,它会打印“From…za”。现在我知道在za到达字符之前,它后面还有更多字符\n?我的程序如何决定只打印“From…za”,而不打印其他字符

提前谢谢你给我添麻烦。
马里奥

我不知道问题出在哪里。你的意思是说它没有在第一行打印2008年1月5日09:14:16星期六的
吗?就是这样!为什么程序停在那里?在搜索“发件人:”时,程序似乎选择了第38行,而不是第1行。试着用“From”来代替它,它应该会修复它。因为法语是我的第一语言,我发现这个问题写得不好。我将对其进行编辑,以澄清我的问题所搜索的内容。正如哈克德所说:您打印的行以
From:
开头,而不仅仅是以
From
开头的行。删除
以获得其他行。