章硕,js & python

夏一一、乔3少 赞同

这不是python的问题,windows的换行是\r\n,unix的是\n,mac的是\r。

这是一个很经典的问题。因为不同系统下默认的换行符不同。字符处理时候,这样的“不同”会带来很大的问题,例如line[-2]和line.strip()会因为平台不同返回不同的值。

解决方法:

Python 2

1)如果不是txt文件,建议用wb和rb来读写。通过二进制读写,不会有换行问题。

2)如果需要明文内容,请用rU来读取(强烈推荐),即U通用换行模式(Universal new line mode)。该模式会把所有的换行符(\r \n \r\n)替换为\n。只支持读入,但是也足够了。这是Python 提供给我们的最好的选择,没有之一。

对比r和rU的结果:

content = file(fn, 'r').read()

# test\r\ntest2

# 这里的换行会因不同系统而不同

content = file(fn, 'rU').read()

# test\ntest2

# 所有的换行都被统一,不分系统

Python 3

请注意:Python 3不推荐用rU模式!

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None, closefd=True)

在Python 3,可以通过open函数的newline参数来控制Universal new line mode:读取时候,不指定newline,则默认开启Universal new line mode,所有\n, \r, or \r\n被默认转换为\n ;写入时,不指定newline,则换行符为各系统默认的换行符(\n, \r, or \r\n, ),指定为newline='\n',则都替换为\n(相当于Universal new line mode);不论读或者写时,newline=''都表示不转换。newline controls how universal newlines works (it only applies to text mode). It can be None, '', '\n', '\r', and '\r\n'. It works as follows:On input, if newline is None, universal newlines mode is enabled. Lines in the input can end in '\n', '\r', or '\r\n', and these are translated into '\n' before being returned to the caller. If it is '', universal newline mode is enabled, but line endings are returned to the caller untranslated. If it has any of the other legal values, input lines are only terminated by the given string, and the line ending is returned to the caller untranslated.

On output, if newline is None, any '\n' characters written are translated to the system default line separator,os.linesep. If newline is '', no translation takes place. If newline is any of the other legal values, any '\n' characters written are translated to the given string.

参考文献:PEP 278 -- Universal Newline Support

python中的换行符是哪个键_Python换行符问题:\r\n还是\n?相关推荐

  1. python中的换行符是哪个键_python的换行符是什么?

    python换行符是什么? python换行符是"\n". Windows换行符是'\r\n', Unix/Linux的换行符为'\n', Mac的换行符为'\r', 在pytho ...

  2. [转载] python中print()函数的用法和end=““不换行详解

    参考链接: Python | print()中的结束参数 需求:打印五个字符,在一行上 代码: i = 0 while i< 5 : i += 1 print(i,end='') 结果: 1 2 ...

  3. python中占位符可以用什么_python占位符都有什么

    python中为什么要使用占位符? python如何在一个字符串中使用两个占位符 1. 使用连接符: + 12 world = "World"print "Hello & ...

  4. python中使用什么注释语句和运算_Python基础知识

    本文标识 :Python_1 本文编辑 : Jack 风 编程工具 : Anaconda.Pycharm 阅读时长 : 12分钟 Python基础学习为什么那么多培训机构?那么多的程序员呢? 很简单啊 ...

  5. python中newfile是干嘛用的_python 文件操作方法

    python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当前Python脚本工作的目录路径: os.getcwd() 返回指定目录下的所有文件和目 ...

  6. python中match方法返回字符串的长度_Python re模块与正则表达式详解

    Python 中使用re模块处理正则表达式,正则表达式主要用来处理文本中的查找,匹配,替换,分割等问题:我们先来看一个问题,切入正则表达式. 问题: 匹配字符串,最少以3个数字开头.使用Python代 ...

  7. python中正则表达式的默认匹配方式为_Python模式匹配与正则表达式

    1.1 不用正则表达式来匹配文本 假设我希望在一个字符串中找到电话号码,电话号码的格式为三个数字,一个短横线,四个数字,一个短横线,四个数字 比如:131-3310-5293和132-2670-986 ...

  8. python中四种进制的输出_Python基础语法和进制

    基础语法 开发种常用的快捷键 键位 功能 ctrl + / 添加注释和取消注释 ctrl + s 保存 ctrl + c 复制 ctrl + v 粘贴 ctrl + x 剪切 ctrl + z 撤销 ...

  9. python中简述对象和类的关系_Python学习笔记(七)对象和类

    什么是对象 Python中所有的数据都是以对象的形式存在,无论是简单的数字类型还是复杂的代码模块.在Python中,当我们想要创建属于自己的对象或者修改已有对象的行为时,才需要关注对象的内部实现细节. ...

  10. python中return返回值为函数名_Python函数返回值

    Python函数返回值教程 同时,Python 函数的返回值不需要在函数的签名上体现,只需要在函数体里面直接使用 return 语句返回即可. Python函数不返回值详解 语法 def func_n ...

最新文章

  1. php邮箱验证laravel接口,Email认证 | Laravel 中文文档 - 码农教程
  2. JavaScript 中回调地狱的今生前世
  3. ZooKeeper实战(三):ZooKeeper实现分布式配置中心、分布式锁、Reactive响应式模型
  4. SQL SERVER 数据导出JSON
  5. java三级报名_java web 学习 --第四天(Java三级考试)
  6. 《python cookbook》chapter 1
  7. Python使用本地代理 proxy
  8. 互斥锁和条件变量【原创总结】
  9. java中id name_关于DOM对象中的id与name的区别
  10. [数据库]日期格式相关参数传递
  11. c语言元素累积的计算,C语言:计算输出给定数组中每相邻两个元素的平均值的平方根之和。...
  12. C++学习一explicit
  13. mysql 程序编码_MySql编码
  14. c++ 类全局变量_static在C和C++中的用法总结
  15. socks v5 协议解析
  16. swift3 下标subscript
  17. Django应用及分布式路由
  18. 欧姆龙CP/CJ系列PLC以太网通讯方案
  19. Highcharts 江湖就这样
  20. Win10浏览器打不开弹出Openwith.exe 没有注册类

热门文章

  1. cartographer探秘之文章索引
  2. cartographer探秘第四章之代码解析(五) --- 后端优化 --- 闭环约束2 --- FastCorrelativeScanMatcher2D
  3. 图像形态学运算之腐蚀-膨胀篇
  4. opencv 写视频时找不到编码器问题解决方法
  5. TreeMap内部实现简介
  6. 统一视角理解实例分割算法:最新进展分析与总结
  7. PCL对点云进行滤波处理并进行颜色可视化
  8. struts2 与 spring 整合
  9. 做项目时自己的心得体会
  10. (BFS) bzoj 1102