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. mybatis 存储过程的写法

    (注意事项: 在使用游标的时候,不能在游标声明之前,使用crud) 存储过程示例 CREATE DEFINER=`root`@`::` PROCEDURE `earnings_proceduce`() ...

  2. django模板系统的基本原则

    写模板,创建template对象,创建 context ,调用render()方法

  3. Activiti6简明教程

    一.为什么选择Activiti 工作流引擎对比 二.核心7大接口.28张表 7大接口 (一)7大接口 RepositoryService:提供一系列管理流程部署和流程定义的API. RuntimeSe ...

  4. Vickers Vane Pump Tips - Vane Pump Maintenance Note

    The    Vickers Vane Pump    describes the matters needing attention in the maintenance of the vane p ...

  5. Jenkins中部署Sonar代码检查

    1 安装并启动sonarqube docker pull sonarqube:7.5-community docker run \ --name sonarqube \ --network ci \ ...

  6. layui二次封装

    最近一直用layui进行页面的重构,这个框架十分适合我们后台人员开发.简单易用,但是layui本身不支持双向绑定,所以很多情况下,我们在支持动态的控件加载时,需要反复刷新.这里我自己封装了一个comm ...

  7. 10. GLOBAL_STATUS 与 SESSION_STATUS

    10. GLOBAL_STATUS 与 SESSION_STATUS 注意 从MySQL 5.7.6开始,show_compatibility_56系统变量的值会影响此处描述的表中的可用信息. 有关详 ...

  8. ubuntu中执行docker info出现警告信息WARNING: No memory limit support 或 WARNING: No swap limit support

    docker info 指令报若下错误:WARNING: No memory limit support 或WARNING: No swap limit support 解决方法: 1.打开/etc/ ...

  9. 如何用纯 CSS 创作一种按钮被瞄准的交互特效

    效果预览 按下右侧的"点击预览"按钮在当前页面预览,点击链接全屏预览. 在线演示 https://codepen.io/zhang-ou/pen/ELWMLr 可交互视频教程 此视 ...

  10. swoole 编程环境安装

    安装linux debian 9.8 购买阿里云vps web目录规划 lnmp/source lnmp/soft lnmp/data 源码安装php apt install gcc make apt ...