如何使用Python进行文件读写操作并设置自定义分隔符?
- 内容介绍
- 文章标签
- 相关推荐
本文共计1410个文字,预计阅读时间需要6分钟。
Python文件读取时默认支持newlines(即换行符),这是指Python在读取文件时会自动处理不同操作系统的换行符差异。无论是从Python的官方文档上获取的信息,还是在Python的源代码中,这一特性都是一致的。
众所周知,python文件读取文件的时候所支持的newlines(即换行符),是指定的。这一点不管是从python的doucuments上还是在python的源码中(作者是参考了python的io版本,并没有阅读C版本),都可以看出来:
if newline is not None and not isinstance(newline, str): raise TypeError("illegal newline type: %r" % (type(newline),)) if newline not in (None, "", "\n", "\r", "\r\n"): raise ValueError("illegal newline value: %r" % (newline,))
好吧,问题来了,如果你恰好是个苦逼的生物狗,正在用python处理所谓的fastq格式的测序结果文件,每次只读一行往往不是你想要的。
本文共计1410个文字,预计阅读时间需要6分钟。
Python文件读取时默认支持newlines(即换行符),这是指Python在读取文件时会自动处理不同操作系统的换行符差异。无论是从Python的官方文档上获取的信息,还是在Python的源代码中,这一特性都是一致的。
众所周知,python文件读取文件的时候所支持的newlines(即换行符),是指定的。这一点不管是从python的doucuments上还是在python的源码中(作者是参考了python的io版本,并没有阅读C版本),都可以看出来:
if newline is not None and not isinstance(newline, str): raise TypeError("illegal newline type: %r" % (type(newline),)) if newline not in (None, "", "\n", "\r", "\r\n"): raise ValueError("illegal newline value: %r" % (newline,))
好吧,问题来了,如果你恰好是个苦逼的生物狗,正在用python处理所谓的fastq格式的测序结果文件,每次只读一行往往不是你想要的。

