python统一的换行符,实现跨平台
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统一的换行符,实现跨平台的更多相关文章
- python 替换windows换行符为unix格式
windows 默认换行符为 \r\n; unix默认换行符为 \n; 所以当win下编辑的脚本在linux下显示末尾多了^M: 换行符修改为同一的unix格式脚本如下: def run(path,f ...
- Python处理文本换行符
源文件每行后面都有回车,所以用下面输出时,中间会多了一行 try: with open("F:\\hjt.txt" ) as f : for line in f: print(li ...
- Git坑换行符自动转换 [转载]
转自https://www.cnblogs.com/zjoch/p/5400251.html 源起 一直想在 GitHub 上发布项目.参与项目,但 Git 这货比较难学啊.买了一本<Git 权 ...
- GitHub 第一坑:换行符自动转换
源起 一直想在 GitHub 上发布项目.参与项目,但 Git 这货比较难学啊.买了一本<Git 权威指南>,翻了几页,妈呀,那叫一个复杂,又是 Cygwin 又是命令行的,吓得我不敢学了 ...
- php换行符
1.需求 统一php换行符 2.实践 使用PHP_EOL替换换行符,保证平台的兼容性. 类似的有DIRECTORY_SEPARATOR 参考文档:http://www.cnblogs.com/code ...
- python 换行符的识别问题,Unix 和Windows 中是不一样的
关于换行符的识别问题,在Unix 和Windows 中是不一样的(分别是n 和rn).默认情况下,Python 会以统一模式处理换行符.这种模式下,在读取文本的时候,Python 可以识别所有的普通换 ...
- python使用platform模块获取系统环境并去除换行符
近来在porting一个网站,企图拿到这个网站的数据来做分析.为了支持多系统环境的正常运行.需要知道当前系统环境的是什么OS? 1.python内置platform库.可以很方便得到当前系统环境时什么 ...
- python 按每行读取文件怎么去掉换行符
python按每行读取文件后,会在每行末尾带上换行符,这样非常不方便后续业务处理逻辑,需要去掉每行的换行符,怎么去掉呢?看下面的案例: >>> a = "hello wor ...
- Python换行符问题:\r\n还是\n?
今天写一个Python脚本去读取一个txt文件时,发现一个很有趣的现象: 如果这个文件是用atom编辑器写的,发现换行符是'\r'.这样一来去逐行读取数据就失效了,因为用open函数 去打开该文件re ...
随机推荐
- Android系统固件定制方式
target_product.mkAndroid系统在构建关于某种产品的固件时,一般会根据特定于该产品的具体target_product.mk来配置生成整个Android系统./target_prod ...
- CentOS7.6 静态IP配置
1:选中安装好的虚拟机,点击“编辑”,然后选择“虚拟网络编辑器(N…)”,如下图所示: 2:选择桥接模式,在桥接到指定的本地网卡即可."确定"保存 3:选中虚拟机,右击虚拟机,选择 ...
- Android开发中JavaBean类和序列化知识的理解
原创文章,转载请注明出处:http://www.cnblogs.com/baipengzhan/p/6296121.html Android开发中,我们经常用到JavaBean类以及序列化的知识,但经 ...
- 16. PLUGINS
16. PLUGINS PLUGINS表提供有关服务器插件的信息. PLUGINS表有以下列: PLUGIN_NAME :用于在诸如INSTALL PLUGIN和UNINSTALL PLUGIN之类的 ...
- 算法导论 第三章 and 第四章
第三章 渐进的基本O().... 常用函数 % 和 // 转换 斯特林近似公式 斐波那契数 第四章 分治策略:分解(递归)--解决(递归触底)--合并 求解递归式的3种方法: 1:代入法(替代法): ...
- laravel(4.2) +Zizaco
操作步骤:https://github.com/Zizaco/entrust/tree/1.0 这篇博客说的蛮详细的:http://blog.boolw.com/?p=241 简化后的步骤 1.在根项 ...
- Java学习之并发多线程理解
1.线程简介: 世间万物会同时完成很多工作,如人体同时进行呼吸.血液循环.思考问题等活动,用户既可以使用计算机听歌也可以使用它打印文件,而这些活动完全可以同时进行,这种思想在Java中称为并发,而将并 ...
- wps左侧显示目录
单击视图----文档结构图,在下拉选项中选择靠左即可,如图所示
- zoj 2722 Head-to-Head Match(两两比赛)
Head-to-Head Match Time Limit: 2 Seconds Memory Limit: 65536 KB Our school is planning to hold ...
- OO第二次作业
第一次作业: 由于第一次作业的调度较为简单,采用FIFO策略,以及不支持捎带功能,因此我的第一次电梯作业并没有设置单独的调度器,而会直接将任务交给电梯,电梯进行调度策略也仅为先运动到people的In ...