6 PEP 278: Universal Newline Support

The three major operating systems used today are Microsoft Windows, Apple's Macintosh OS, and the various Unix derivatives. A minor irritation of cross-platform work is that these three platforms all use different characters to mark the ends of lines in text files.  Unix uses the linefeed (ASCII character 10), MacOS uses the carriage return (ASCII character 13), and Windows uses a two-character sequence of a carriage return plus a newline.

Python's file objects can now support end of line conventions other than the one followed by the platform on which Python is running. Opening a file with the mode 'U' or 'rU' will open a file for reading in universal newline mode. All three line ending conventions will be translated to a "\n" in the strings returned by the various file methods such as read() and readline().

Universal newline support is also used when importing modules and when executing a file with the execfile() function. This means that Python modules can be shared between all three operating systems without needing to convert the line-endings.

This feature can be disabled when compiling Python by specifying the --without-universal-newlines switch when running Python's configure script.

标记文本文档一行结束的符号:

1、unix LF(ascii 10;0x10)

2、mac CR(ascii 13;0x0C)

3、windows (CRLF 0x0c10)

python读取文件的时候使用U或者rU,来实现跨平台,它把所有的符号统一转换为\n

with open(filename,'rU') as f:

f.read()/f.readling()

https://docs.python.org/2.3/whatsnew/node7.html

python统一的换行符,实现跨平台的更多相关文章

  1. python 替换windows换行符为unix格式

    windows 默认换行符为 \r\n; unix默认换行符为 \n; 所以当win下编辑的脚本在linux下显示末尾多了^M: 换行符修改为同一的unix格式脚本如下: def run(path,f ...

  2. Python处理文本换行符

    源文件每行后面都有回车,所以用下面输出时,中间会多了一行 try: with open("F:\\hjt.txt" ) as f : for line in f: print(li ...

  3. Git坑换行符自动转换 [转载]

    转自https://www.cnblogs.com/zjoch/p/5400251.html 源起 一直想在 GitHub 上发布项目.参与项目,但 Git 这货比较难学啊.买了一本<Git 权 ...

  4. GitHub 第一坑:换行符自动转换

    源起 一直想在 GitHub 上发布项目.参与项目,但 Git 这货比较难学啊.买了一本<Git 权威指南>,翻了几页,妈呀,那叫一个复杂,又是 Cygwin 又是命令行的,吓得我不敢学了 ...

  5. php换行符

    1.需求 统一php换行符 2.实践 使用PHP_EOL替换换行符,保证平台的兼容性. 类似的有DIRECTORY_SEPARATOR 参考文档:http://www.cnblogs.com/code ...

  6. python 换行符的识别问题,Unix 和Windows 中是不一样的

    关于换行符的识别问题,在Unix 和Windows 中是不一样的(分别是n 和rn).默认情况下,Python 会以统一模式处理换行符.这种模式下,在读取文本的时候,Python 可以识别所有的普通换 ...

  7. python使用platform模块获取系统环境并去除换行符

    近来在porting一个网站,企图拿到这个网站的数据来做分析.为了支持多系统环境的正常运行.需要知道当前系统环境的是什么OS? 1.python内置platform库.可以很方便得到当前系统环境时什么 ...

  8. python 按每行读取文件怎么去掉换行符

    python按每行读取文件后,会在每行末尾带上换行符,这样非常不方便后续业务处理逻辑,需要去掉每行的换行符,怎么去掉呢?看下面的案例: >>> a = "hello wor ...

  9. Python换行符问题:\r\n还是\n?

    今天写一个Python脚本去读取一个txt文件时,发现一个很有趣的现象: 如果这个文件是用atom编辑器写的,发现换行符是'\r'.这样一来去逐行读取数据就失效了,因为用open函数 去打开该文件re ...

随机推荐

  1. Java从入门到放弃18---Map集合/HashMap/LinkedHashMap/TreeMap/集合嵌套/Collections工具类常用方法

    Java从入门到放弃18—Map集合/HashMap/LinkedHashMap/TreeMap/集合嵌套/Collections工具类常用方法01 Map集合Map集合处理键值映射关系的数据为了方便 ...

  2. linux之awk命令

    一.awk的内置参数 $0:表示整个当前行 $1:每行第一个字段 $2:每行第二个字段 $n:每行第n个字段 awk的参数:分隔符 -F separator 设定分隔符(默认为空格) 打印单个字段: ...

  3. MySQL-04 高级特性

    学习要点 MySQL系统函数 MySQL视图 MySQL存储过程 MySQL自定义函数 MySQL触发器 MySQL事务 MySQL事件 MySQL系统函数 数学函数 获取整数的函数:CEIL(x). ...

  4. No-10.高级变量类型

    高级变量类型 目标 列表 元组 字典 字符串 公共方法 变量高级 知识点回顾 Python 中数据类型可以分为 数字型 和 非数字型 数字型 整型 (int) 浮点型(float) 布尔型(bool) ...

  5. 数据结构——RMQ

    RMQ 今天临放学前终于是学会了RMQ,特此写一篇题解来缅怀 RMQ是一种数据结构,用途是查询区间内最大值或最小值 或者你所要求的任意条件,主要思想是二进制的思想,其中还用到了dp的思想, 是一种非常 ...

  6. koa源码解读

    koa是有express原班人马打造的基于node.js的下一代web开发框架.koa 1.0使用generator实现异步,相比于回调简单和优雅和不少.koa团队并没有止步于koa 1.0, 随着n ...

  7. centos 7 安装 docker(详细)

    更新源 mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup wget -O/etc/yum.re ...

  8. mysql主从同步 change master to配置

    CHANGE MASTER TO MASTER_HOST='10.0.0.52', MASTER_PORT=3308, MASTER_AUTO_POSITION=1, MASTER_USER='rep ...

  9. Java中的事务——全局事务与本地事务

    转载,原文来源 http://www.hollischuang.com Java事务的类型有三种:JDBC事务.JTA(Java Transaction API)事务.容器事务.这是从事务的实现角度区 ...

  10. 【BZOJ 1588】[HNOI2002] 营业额统计(Treap)

    Description 营业额统计 Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司的账本,账本上记录了公司成立以来每 ...