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. NetBeans 默认编码修改方法

    如果要NetBeans用UTF-8对文件进行解码,需要修改配置文件,具体方法如下: 1. 找到你的Netbeans安装目录下的etc文件夹,如D:\Program Files\NetBeans 8.2 ...

  2. 基于纯注解的spring开发的介绍

    几个核心注解的介绍1.@Configuration它的作用是:将一个java类修饰为==配置文件==,在这个java类进行组件注册1package com.kkb.config; import org ...

  3. js获取农历

    上一篇我们对upupoo网页壁纸改造时用到了农历,upupoo(网页壁纸)自主修改一:农历,这里记一下笔记: 获取当前农历的js 主js: //农历 var CalendarData=new Arra ...

  4. Elasticsearch 索引管理和内核探秘

    1. 创建索引,修改索引,删除索引 //创建索引 PUT /my_index { "settings": { , }, "mappings": { " ...

  5. p1036 选数(不详细勿看,递归)

    题目:传送门 这题,不会做,而且看了好久才看懂题解的,然后在题解的基础上补了一个 if(start>end) return 0 感觉这样对于我更直观 转载自:大神博客的传送门,点击进入 先声明, ...

  6. svn服务

    svn服务实战应用指南 1.1     svn介绍 什么是svn? svn(sub-version)是近年来崛起的非常优秀的版本管理工具,与cvs管理工具一样,svn是一个跨平台的开源的版本控制系统, ...

  7. tomcat7使用dbcp连接池遇到的坑

    项目部署在tomcat后每隔一段时间便会报错 Cause: java.sql.SQLException: Could not retrieve transation read-only status ...

  8. idea Error:(65, 27) java: 未结束的字符串文字

    今天在使用IDEA的时候,出现了这个错误,原因项目文件编码不一致导致的,解决方法是: 将项目的文件编码全改成一致(UTF-8),如下图所示:

  9. 嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av13570243/?from=search&seid=15873837810484552531 中的1-14讲

    #coding=gbk#嵩天老师的零基础Python笔记:https://www.bilibili.com/video/av13570243/?from=search&seid=1587383 ...

  10. Saving James Bond - Easy Version 原创 2017年11月23日 13:07:33

    06-图2 Saving James Bond - Easy Version(25 分) This time let us consider the situation in the movie &q ...