为什么我不能在打开的文件上调用 read() 两次?
对于我正在做的一个练习,我尝试使用 read()
方法两次读取给定文件的内容.奇怪的是,当我第二次调用它时,它似乎没有将文件内容作为字符串返回?
For an exercise I'm doing, I'm trying to read the contents of a given file twice using the read()
method. Strangely, when I call it the second time, it doesn't seem to return the file content as a string?
这是代码
f = f.open()
# get the year
match = re.search(r'Popularity in (d+)', f.read())
if match:
print match.group(1)
# get all the names
matches = re.findall(r'<td>(d+)</td><td>(w+)</td><td>(w+)</td>', f.read())
if matches:
# matches is always None
当然我知道这不是最有效或最好的方式,这不是这里的重点.关键是,为什么我不能调用 read()
两次?我必须重置文件句柄吗?或者关闭/重新打开文件以执行此操作?
Of course I know that this is not the most efficient or best way, this is not the point here. The point is, why can't I call read()
twice? Do I have to reset the file handle? Or close / reopen the file in order to do that?
调用 read()
读取整个文件并将读取光标留在文件末尾(没有更多内容可读取)).如果您希望一次读取一定数量的行,您可以使用 readline()
、readlines()
或使用 for line in handle 遍历行:
.
Calling read()
reads through the entire file and leaves the read cursor at the end of the file (with nothing more to read). If you are looking to read a certain number of lines at a time you could use readline()
, readlines()
or iterate through lines with for line in handle:
.
直接回答你的问题,一旦文件被读取,使用 read()
你可以使用 seek(0)
将读取光标返回到开始文件(文档在这里).如果您知道文件不会太大,您还可以将 read()
输出保存到一个变量中,在您的 findall 表达式中使用它.
To answer your question directly, once a file has been read, with read()
you can use seek(0)
to return the read cursor to the start of the file (docs are here). If you know the file isn't going to be too large, you can also save the read()
output to a variable, using it in your findall expressions.
附言.完成后不要忘记关闭文件;)
Ps. Dont forget to close the file after you are done with it ;)