zoukankan      html  css  js  c++  java
  • 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 " " 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,来实现跨平台,它把所有的符号统一转换为

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

         f.read()/f.readling()

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

  • 相关阅读:
    Android中的跨进程通信方法实例及特点分析(二):ContentProvider
    智能交通焕发勃勃生机,未来会呈现哪些巨变?
    VS2008下编译boost_1_47_0
    windows下用vs2008和boost结合编译程序
    查表法计算CRC16校验值
    MFC读写配置文件
    VS2008快捷键_大全
    关于VS2008中的targetver.h文件
    VC++ 实验室仿真虚拟仪器
    OLEDB简介
  • 原文地址:https://www.cnblogs.com/shengulong/p/8134277.html
Copyright © 2011-2022 走看看