在mac上打开python上的文本文件
我正试图在mac上用python编写一个程序,允许用户搜索数据库。我无法打开、查找或读取所附的文本文件 我使用了:在mac上打开python上的文本文件,python,text-files,Python,Text Files,我正试图在mac上用python编写一个程序,允许用户搜索数据库。我无法打开、查找或读取所附的文本文件 我使用了: import os with open('a3-example-data.txt', 'r') as f: f.readline() for line in f: if 'Sample Text' in line: print "I have found it" f.seek(0)
import os
with open('a3-example-data.txt', 'r') as f:
f.readline()
for line in f:
if 'Sample Text' in line:
print "I have found it"
f.seek(0)
f.readline()
for line in f:
if 'Time Taken' in line:
print line
print ' '.join(line.split())
f.close()
及
但不断收到错误信息。请帮助我:(您的代码在许多方面都有缺陷,我猜错误是在您返回到主迭代时出现的,而您返回到
0
,使主迭代不同步
# you do not need the os module in your code. Useless import
import os
with open('a3-example-data.txt', 'r') as f:
### the f.readline() is only making you skip the first line.
### Are you doing it on purpose?
f.readline()
for line in f:
if 'Sample Text' in line:
print "I have found it"
### seeking back to zero,
f.seek(0)
### skipping a line
f.readline()
### iterating over the file again,
### while shadowing the current iteration
for line in f:
if 'Time Taken' in line:
print line
print ' '.join(line.split()) # why are you joining what you just split?
### and returning to the main iteration which will get broken
### because of the seek(0) within
### does not make much sense.
### you're using the context manager, so once you exit the `with` block, the file is closed
### no need to double close it!
f.close()
因此,在不了解您的目标的情况下,以下是我对您的算法的看法:
import os
with open('a3-example-data.txt', 'r') as f:
f.readline()
for line in f:
if 'Sample Text' in line:
print "I have found it"
break
f.seek(0)
f.readline()
for line in f:
if 'Time Taken' in line:
print line
您的代码在很多方面都有缺陷,我猜当您返回到主迭代时会出现错误,而您返回到
0
,使主迭代不同步
# you do not need the os module in your code. Useless import
import os
with open('a3-example-data.txt', 'r') as f:
### the f.readline() is only making you skip the first line.
### Are you doing it on purpose?
f.readline()
for line in f:
if 'Sample Text' in line:
print "I have found it"
### seeking back to zero,
f.seek(0)
### skipping a line
f.readline()
### iterating over the file again,
### while shadowing the current iteration
for line in f:
if 'Time Taken' in line:
print line
print ' '.join(line.split()) # why are you joining what you just split?
### and returning to the main iteration which will get broken
### because of the seek(0) within
### does not make much sense.
### you're using the context manager, so once you exit the `with` block, the file is closed
### no need to double close it!
f.close()
因此,在不了解您的目标的情况下,以下是我对您的算法的看法:
import os
with open('a3-example-data.txt', 'r') as f:
f.readline()
for line in f:
if 'Sample Text' in line:
print "I have found it"
break
f.seek(0)
f.readline()
for line in f:
if 'Time Taken' in line:
print line
您的代码在很多方面都有缺陷,我猜当您返回到主迭代时会出现错误,而您返回到
0
,使主迭代不同步
# you do not need the os module in your code. Useless import
import os
with open('a3-example-data.txt', 'r') as f:
### the f.readline() is only making you skip the first line.
### Are you doing it on purpose?
f.readline()
for line in f:
if 'Sample Text' in line:
print "I have found it"
### seeking back to zero,
f.seek(0)
### skipping a line
f.readline()
### iterating over the file again,
### while shadowing the current iteration
for line in f:
if 'Time Taken' in line:
print line
print ' '.join(line.split()) # why are you joining what you just split?
### and returning to the main iteration which will get broken
### because of the seek(0) within
### does not make much sense.
### you're using the context manager, so once you exit the `with` block, the file is closed
### no need to double close it!
f.close()
因此,在不了解您的目标的情况下,以下是我对您的算法的看法:
import os
with open('a3-example-data.txt', 'r') as f:
f.readline()
for line in f:
if 'Sample Text' in line:
print "I have found it"
break
f.seek(0)
f.readline()
for line in f:
if 'Time Taken' in line:
print line
您的代码在很多方面都有缺陷,我猜当您返回到主迭代时会出现错误,而您返回到
0
,使主迭代不同步
# you do not need the os module in your code. Useless import
import os
with open('a3-example-data.txt', 'r') as f:
### the f.readline() is only making you skip the first line.
### Are you doing it on purpose?
f.readline()
for line in f:
if 'Sample Text' in line:
print "I have found it"
### seeking back to zero,
f.seek(0)
### skipping a line
f.readline()
### iterating over the file again,
### while shadowing the current iteration
for line in f:
if 'Time Taken' in line:
print line
print ' '.join(line.split()) # why are you joining what you just split?
### and returning to the main iteration which will get broken
### because of the seek(0) within
### does not make much sense.
### you're using the context manager, so once you exit the `with` block, the file is closed
### no need to double close it!
f.close()
因此,在不了解您的目标的情况下,以下是我对您的算法的看法:
import os
with open('a3-example-data.txt', 'r') as f:
f.readline()
for line in f:
if 'Sample Text' in line:
print "I have found it"
break
f.seek(0)
f.readline()
for line in f:
if 'Time Taken' in line:
print line
这个错误消息,是什么?还有,我不确定你为什么认为第二个代码段会工作,它有语法错误。这个错误消息,它是什么?还有,我不确定你为什么认为第二个代码段会工作,它有语法错误。这个错误消息,它是什么?还有,我不确定你为什么认为第二个代码段会工作,它有语法错误。这个错误消息是什么?而且,我不知道你为什么认为第二个代码段会工作,它有语法错误。