您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python-如何跳至巨大文本文件中的特定行?

Python-如何跳至巨大文本文件中的特定行?

由于你不知道换行符在哪里,因此无法至少一次不读入文件就无法跳转。你可以执行以下操作:

# Read in the file once and build a list of line offsets
line_offset = []
offset = 0
for line in file:
    line_offset.append(offset)
    offset += len(line)
file.seek(0)

# Now, to skip to line n (with the first line being line 0), just do
file.seek(line_offset[n])
python 2022/1/1 18:22:09 有508人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶