Python 连续读取文件
Python Consecutive Reads From File
我有一个正在从文件中读取的 Python 脚本。
第一个命令计算行数。第二个打印第二行,尽管第二个不工作。
lv_file = open("filename.txt", "rw+")
# count the number of lines =================================
lv_cnt = 0
for row in lv_file.xreadlines():
lv_cnt = lv_cnt + 1
# print the second line =====================================
la_lines = la_file.readlines()
print la_lines[2]
lv_file.close()
当我这样写它时它可以工作,但我不明白为什么我必须关闭文件并重新打开它才能让它工作。我是否滥用了某种功能?
lv_file = open("filename.txt", "rw+")
# count the number of lines =================================
lv_cnt = 0
for row in lv_file.xreadlines():
lv_cnt = lv_cnt + 1
lv_file.close()
lv_file = open("filename.txt", "rw+")
# print the second line =====================================
la_lines = la_file.readlines()
print la_lines[2]
lv_file.close()
文件对象是一个迭代器。一旦你遍历了所有的行,迭代器就用完了,进一步的读取将无济于事。
为避免关闭并重新打开文件,您可以使用 seek
倒回到开头:
lv_file.seek(0)
你要的是file.seek()
:
示例:(基于您的代码)
lv_file = open("filename.txt", "rw+")
# count the number of lines =================================
lv_cnt = 0
for row in lv_file.xreadlines():
lv_cnt = lv_cnt + 1
lv_file.seek(0) # reset file pointer
# print the second line =====================================
la_lines = la_file.readlines()
print la_lines[2]
lv_file.close()
这会将文件指针重置回其起始位置。
pydoc file.seek
:
seek(offset, whence=SEEK_SET)
Change the stream position to the
given byte offset. offset is interpreted relative to the position
indicated by whence. Values for whence are:
SEEK_SET or 0 – start of the stream (the default); offset should be
zero or positive SEEK_CUR or 1 – current stream position; offset may
be negative SEEK_END or 2 – end of the stream; offset is usually
negative Return the new absolute position.
New in version 2.7: The SEEK_* constants
更新: 一种更好的计算次数的方法。迭代文件中的行数并且只关心第二行:
def nth_line_and_count(filename, n):
"""Return the nth line in a file (zero index) and the no. of lines"""
count = 0
with open(filename, "r") as f:
for i, line in enumerate(f):
count += 1
if i == n:
value = line
return count, value
nlines, line = nth_line_and_count("filename.txt", 1)
由于 xreadlines() 保留指向它发送给您的最后一行的指针,因此当您执行
la_lines = la_file.readlines()
它基本上记住了它给你的最后一行的索引。
当您关闭文件然后打开它时,它会创建一个新的迭代器,并再次指向第 0 行。
我有一个正在从文件中读取的 Python 脚本。 第一个命令计算行数。第二个打印第二行,尽管第二个不工作。
lv_file = open("filename.txt", "rw+")
# count the number of lines =================================
lv_cnt = 0
for row in lv_file.xreadlines():
lv_cnt = lv_cnt + 1
# print the second line =====================================
la_lines = la_file.readlines()
print la_lines[2]
lv_file.close()
当我这样写它时它可以工作,但我不明白为什么我必须关闭文件并重新打开它才能让它工作。我是否滥用了某种功能?
lv_file = open("filename.txt", "rw+")
# count the number of lines =================================
lv_cnt = 0
for row in lv_file.xreadlines():
lv_cnt = lv_cnt + 1
lv_file.close()
lv_file = open("filename.txt", "rw+")
# print the second line =====================================
la_lines = la_file.readlines()
print la_lines[2]
lv_file.close()
文件对象是一个迭代器。一旦你遍历了所有的行,迭代器就用完了,进一步的读取将无济于事。
为避免关闭并重新打开文件,您可以使用 seek
倒回到开头:
lv_file.seek(0)
你要的是file.seek()
:
示例:(基于您的代码)
lv_file = open("filename.txt", "rw+")
# count the number of lines =================================
lv_cnt = 0
for row in lv_file.xreadlines():
lv_cnt = lv_cnt + 1
lv_file.seek(0) # reset file pointer
# print the second line =====================================
la_lines = la_file.readlines()
print la_lines[2]
lv_file.close()
这会将文件指针重置回其起始位置。
pydoc file.seek
:
seek(offset, whence=SEEK_SET)
Change the stream position to the given byte offset. offset is interpreted relative to the position indicated by whence. Values for whence are:SEEK_SET or 0 – start of the stream (the default); offset should be zero or positive SEEK_CUR or 1 – current stream position; offset may be negative SEEK_END or 2 – end of the stream; offset is usually negative Return the new absolute position.
New in version 2.7: The SEEK_* constants
更新: 一种更好的计算次数的方法。迭代文件中的行数并且只关心第二行:
def nth_line_and_count(filename, n):
"""Return the nth line in a file (zero index) and the no. of lines"""
count = 0
with open(filename, "r") as f:
for i, line in enumerate(f):
count += 1
if i == n:
value = line
return count, value
nlines, line = nth_line_and_count("filename.txt", 1)
由于 xreadlines() 保留指向它发送给您的最后一行的指针,因此当您执行
la_lines = la_file.readlines()
它基本上记住了它给你的最后一行的索引。 当您关闭文件然后打开它时,它会创建一个新的迭代器,并再次指向第 0 行。