我正在处理一个很大的文件,其中包含python中的原始文本。文件内容具有以下格式:
- 每个记录之间用
point_separator
分隔
- 每个字段用
field_separator
分隔。
new record
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
call
point_separator
new record
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
call
point_separator
new record
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
another field
field_separator
call
point_separator
以前,我是使用python文件打开API调用读取完整文件的:
with open(file_name,mode='rt',encoding='utf-8') as reader:
text = reader.read()
objects = text.strip().split('point_separator')
......
但是,当文件很大(即20GB)并且我正在包含MemoryError
RAM的计算机中进行处理时,此错误会以16GB
失败。
问题是我无法逐行读取此文件,因为我必须基于field_separator
收集所有字段,直到看到point_separator
。
有没有一种方法可以使OS使用分页并且可以透明地进行处理?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。