我修改了markbyers的建议,这样我们就可以在Python中用NUL分隔的行来读取行文件。这种方法逐行读取潜在的大文件,并且应该更节省内存。下面是Python代码(带注释):import sys

# Variables for "fileReadLine()"

inputFile = sys.stdin # The input file. Use "stdin" as an example for receiving data from pipe.

lines = [] # Extracted complete lines (delimited with "inputNewline").

partialLine = '' # Extracted last non-complete partial line.

inputNewline="\0" # Newline character(s) in input file.

outputNewline="\n" # Newline character(s) in output lines.

readSize=8192 # Size of read buffer.

# End - Variables for "fileReadLine()"

# This function reads NUL delimited lines sequentially and is memory efficient.

def fileReadLine():

"""Like the normal file readline but you can set what string indicates newline.

The newline string can be arbitrarily long; it need not be restricted to a

single character. You can also set the read size and control whether or not

the newline string is left on the end of the read lines. Setting

newline to '\0' is particularly good for use with an input file created with

something like "os.popen('find -print0')".

"""

# Declare that we want to use these related global variables.

global inputFile, partialLine, lines, inputNewline, outputNewline, readSize

if lines:

# If there is already extracted complete lines, pop 1st llne from lines and return that line + outputNewline.

line = lines.pop(0)

return line + outputNewline

# If there is NO already extracted complete lines, try to read more from input file.

while True: # Here "lines" must be an empty list.

charsJustRead = inputFile.read(readSize) # The read buffer size, "readSize", could be changed as you like.

if not charsJustRead:

# Have reached EOF.

if partialLine:

# If partialLine is not empty here, treat it as a complete line and copy and return it.

popedPartialLine = partialLine

partialLine = "" # partialLine is now copied for return, reset it to an empty string to indicate that there is no more partialLine to return in later "fileReadLine" attempt.

return popedPartialLine # This should be the last line of input file.

else:

# If reached EOF and partialLine is empty, then all the lines in input file must have been read. Return None to indicate this.

return None

partialLine += charsJustRead # If read buffer is not empty, add it to partialLine.

lines = partialLine.split(inputNewline) # Split partialLine to get some complete lines.

partialLine = lines.pop() # The last item of lines may not be a complete line, move it to partialLine.

if not lines:

# Empty "lines" means that we must NOT have finished read any complete line. So continue.

continue

else:

# We must have finished read at least 1 complete llne. So pop 1st llne from lines and return that line + outputNewline (exit while loop).

line = lines.pop(0)

return line + outputNewline

# As an example, read NUL delimited lines from "stdin" and print them out (using "\n" to delimit output lines).

while True:

line = fileReadLine()

if line is None: break

sys.stdout.write(line) # "write" does not include "\n".

sys.stdout.flush()

希望有帮助。在

python读取txt文档判断某一个值是否为nul_Python如何读取以NUL分隔的行的文件?相关推荐

  1. Python读取.txt文档内容/读取text文件内容

    Python读取.txt文档内容/读取text文件内容; 读取txt后缀文件的内容的两种方式,简约易懂,复制粘贴即可食用,节约你宝贵的时间 文档全部数据一次性读取 # -*- coding: UTF- ...

  2. matlab通过变量调用txt文件路径,MATLAB使用load函数读取txt文档

    MATLAB在数据处理方面应用非常广泛.在众多的专业领域里,把各种手段获取的数据存入txt文档中非常常见.如果txt文件中数据为矩阵形式,通常可以采用简单的load命令来读取数据,然后在MATLAB中 ...

  3. 「 C++ MFC 」“读取.txt文档实例”讲解

    一.前言 本博客在MFC工程中,完成了对"读取.txt文档"功能的实战. 二.技术实现 整个程序分为三个过程:开始读文档.应用文档和停止读文档.         1. 开始读文档 ...

  4. javaScript读取txt文档的FSO对象和ADODB.Stream对象

    转自:http://blog.163.com/chaosumin@126/blog/static/86387403201092793610651 在使用JavaScript读取txt文档时,一般使用j ...

  5. C++读取TXT文档,分行分段解析,通过UDP组播发送

    TXT文档格式 航点序号 经度° 纬度° 高度m 速度m/s 悬停时间s 标志字节 1 114.363945 38.056252 20 2 5 1 2 114.363697 38.055994 20 ...

  6. python: 在txt文档开始位置插入数据

    python: 在txt文档开始位置插入数据 python: 在txt文档开始位置插入数据 常见的在文章末尾追加数据很常见,在开头追加数据不常遇见 代码如下 代码片. import ospath = ...

  7. PHP如何读取txt文档域名集,并且生成数组/随机显示一个

    最近有朋友反馈我一个问题看我是否可以帮忙解决:需求就是网页需要跳转代码,用PHP写,读取一个域名集合包(也就是TXT文档),里面方着很多域名,让每访问一次随机跳转一个域名,怕是违法应用,没有答应帮忙, ...

  8. 利用python读取txt文档

    TXT_PATH='/data/benchmark/jingqu_30wan_1002/bm_files/remove100/result.txt' with open(TXT_PATH, 'r') ...

  9. C# 读取.txt文档和.docx文档。npoi读取word文档

    1 NuGet包安装npoi 2 安装npoi 3 获取txt文档内容,不需要安装npoi.filePath是相对路径 public static string ReadFileContent(str ...

最新文章

  1. ios相机黑边_黑边很碍眼很难看 苹果iPhone6黑边有什么用呢?
  2. linux系统启动过程(三)
  3. 测试网页速度性能的利器-Yslow和page speed
  4. Ubuntu Linux配置Nginx+MySQL+PHP+phpMyAdmin详细步骤
  5. JavaScript实现按位运算符乘以无符号数multiplyUnsigned算法(附完整源码)
  6. 软件测试功能测试都有哪一些,【软件测试基础】功能测试常用方法都有哪些
  7. How is SCM product maintenance tcode redirecting access to MARA
  8. 【飞秋】在SPItemEventReceiver中使用BeforeProperties和AfterProperties
  9. nginx开发(二)配置mp4文件在线播放
  10. SQL Server 2008 安装或卸载时提示“重启计算机失败的解决办法(转)
  11. java生成缩略图,接收图片,按指定宽高或按比例生成缩略图
  12. mysql能管理多大的硬盘,Mysql----查看数据库,表占用磁盘大小
  13. oracle中t,Oracle中如何用T
  14. 记一次坑爹的RSA旅程____快哭了555555555(来自实验吧的warmup的wp和感想)
  15. 做B2BB2C电商商城有哪些运营模式?
  16. springboot整合mybatis拦截器分页
  17. LLVM中的pass及其管理机制
  18. ultravnc服务器不接收消息,远程控制软件UltraVNC的教程
  19. OpenGL_Qt学习笔记之_03(平面图形的着色和旋转)
  20. windows编译 zlib

热门文章

  1. 操作系统之虚拟化CPU(一)介绍
  2. Ajax跨域请求(一):什么是CORS
  3. java日志系统介绍
  4. 敏捷方法:什么是软件测试中的敏捷模式?
  5. Android 多通道录音笔记
  6. 为什么移动计算比移动数据更便宜?
  7. imagepreview使用案例_展示组件 ImagePreview 图片预览 - 闪电教程JSRUN
  8. IE以及其他浏览器主页被劫持到www.2345.com/?kunown的解决办法
  9. 虚拟机与主机ssh连接
  10. 智能停车场ARM工控主板应用