文章首发及后续更新:https://mwhls.top/2935.html
新的更新内容请到mwhls.top查看。
无图/无目录/格式错误/更多相关请到上方的文章首发页面查看。

stackoverflow热门问题目录

如有翻译问题欢迎评论指出,谢谢。

以后换个标题,原来的开头太长了,而且格式有点固定,毕竟说不定我以后还会找点其它的翻。

目录
1. Python怎么去除末尾的换行符?
2. How can I remove a trailing newline?

Python怎么去除末尾的换行符?

  • RidingThisToTheTop asked:

    • Pyhon中与Perl的chomp等效的函数有哪些?chomp能实现移除新行中最后的字符。
  • Answers:

    • Ryan Ginstrom – vote: 2094

    • rstrip()可以实现(见Python 2 与 Python 3文档)

    • >>> ‘test string\n’.rstrip()
      ‘test string’

    • Python的rstrip()函数默认分割所有的末尾空白符,而不像Perl的chomp只移除换行符。

    • >>> ‘test string \n \r\n\n\r \n\n’.rstrip()
      ‘test string’

    • 只分割换行符:

    • >>> ‘test string \n \r\n\n\r \n\n’.rstrip(’\n’)
      ‘test string \n \r\n\n\r ‘

    • Python中还有类似的函数,strip()lstrip()

    • >>> s = " \n\r\n \n abc def \n\r\n \n "
      >>> s.strip()‘abc def’
      >>> s.lstrip()
      ‘abc def \n\r\n \n ‘
      >>> s.rstrip()
      ’ \n\r\n \n abc def’

    • Mike – vote: 178

    • Python中,可以通过splitlines()来消除文本中的换行符。

    • >>> text = “line 1\nline 2\r\nline 3\nline 4”
      >>> text.splitlines()
      [‘line 1’, ‘line 2’, ‘line 3’, ‘line 4’]

    • Sameer Siruguri – vote: 159

    • 删除EOL(end-of-line)字符’\r’’\n’的公认做法是使用字符串函数rstrip()。Mac、Windows、Unix的EOL字符删除示例如下:

    • >>> ‘Mac EOL\r’.rstrip(’\r\n’)
      ‘Mac EOL’
      >>> ‘Windows EOL\r\n’.rstrip(’\r\n’)
      ‘Windows EOL’
      >>> ‘Unix EOL\n’.rstrip(’\r\n’)
      ‘Unix EOL’

    • 使用参数为’\r\n’rstrip()可以去除文本末尾的’\r’’\n’。所以上面的三种情况都能正常处理。

    • 不过,Python的strip()处理在少数情况下还是有不足的。例如,我以前有处理过一个包含HL7信息的文件(HL7标准需要以’\r’作为EOL字符)。而我的Windows又会在文本末尾加入Windows的EOL字符’\r\n’,所以每行的结尾就变成了’\r\r\n’。使用rstrip(’\r\n’)会把所有的’\r’’\n’删除,但我不希望这样。为了处理这个问题,我是只对最后的两个字符使用rstrip()函数。

    • 要注意,Python的strip()与Perl的chomp不同,strip()会去除字符串末尾的所有特殊字符,不止一个:

    • >>> “Hello\n\n\n”.rstrip("\n")
      “Hello"


How can I remove a trailing newline?

  • RidingThisToTheTop asked:

    • What is the Python equivalent of Perl\’s chomp function, which removes the last character of a string if it is a newline?
      Pyhon中与Perl的chomp等效的函数有哪些?chomp能实现移除新行中最后的字符。
  • Answers:

    • Ryan Ginstrom – vote: 2094

    • Try the method rstrip() (see doc Python 2 and Python 3)
      rstrip()可以实现(见Python 2 与 Python 3文档)

    • >>> ‘test string\n’.rstrip()
      ‘test string’

    • Python\’s rstrip() method strips all kinds of trailing whitespace by default, not just one newline as Perl does with chomp.
      Python的rstrip()函数默认分割所有的末尾空白符,而不像Perl的chomp只移除新行。

    • >>> ‘test string \n \r\n\n\r \n\n’.rstrip()
      ‘test string’

    • To strip only newlines:
      只分割新行:

    • >>> ‘test string \n \r\n\n\r \n\n’.rstrip(’\n’)
      ‘test string \n \r\n\n\r ‘

    • There are also the methods strip(), lstrip() and strip():
      Python中还有类似的函数,strip()lstrip()

    • >>> s = " \n\r\n \n abc def \n\r\n \n "
      >>> s.strip()‘abc def’
      >>> s.lstrip()
      ‘abc def \n\r\n \n ‘
      >>> s.rstrip()
      ’ \n\r\n \n abc def’

    • Mike – vote: 178

    • And I would say the pythonic way to get lines without trailing newline characters is splitlines().
      Python中,可以通过splitlines()来消除文本中的换行符。

    • >>> text = “line 1\nline 2\r\nline 3\nline 4”
      >>> text.splitlines()
      [‘line 1’, ‘line 2’, ‘line 3’, ‘line 4’]

    • Sameer Siruguri – vote: 159

    • The canonical way to strip end-of-line (EOL) characters is to use the string rstrip() method removing any trailing \r or \n. Here are examples for Mac, Windows, and Unix EOL characters.
      删除EOL(end-of-line)字符’\r’’\n’的公认做法是使用字符串函数rstrip()。Mac、Windows、Unix的EOL字符删除示例如下:

    • >>> ‘Mac EOL\r’.rstrip(’\r\n’)
      ‘Mac EOL’
      >>> ‘Windows EOL\r\n’.rstrip(’\r\n’)
      ‘Windows EOL’
      >>> ‘Unix EOL\n’.rstrip(’\r\n’)
      ‘Unix EOL’

    • Using \’\r\n\’ as the parameter to rstrip means that it will strip out any trailing combination of \’\r\’ or \’\n\’. That\’s why it works in all three cases above.
      使用参数为’\r\n’rstrip()可以去除文本末尾的’\r’’\n’。所以上面的三种情况都能正常处理。

    • This nuance matters in rare cases. For example, I once had to process a text file which contained an HL7 message. The HL7 standard requires a trailing \’\r\’ as its EOL character. The Windows machine on which I was using this message had appended its own \’\r\n\’ EOL character. Therefore, the end of each line looked like \’\r\r\n\’. Using rstrip(\’\r\n\’) would have taken off the entire \’\r\r\n\’ which is not what I wanted. In that case, I simply sliced off the last two characters instead.
      不过,Python的strip()处理在少数情况下还是有不足的。例如,我以前有处理过一个包含HL7信息的文件(HL7标准需要以’\r’作为EOL字符)。而我的Windows又会在文本末尾加入Windows的EOL字符’\r\n’,所以每行的结尾就变成了’\r\r\n’。使用rstrip(’\r\n’)会把所有的’\r’’\n’删除,但我不希望这样。为了处理这个问题,我是只对最后的两个字符使用rstrip()函数。

    • Note that unlike Perl\’s chomp function, this will strip all specified characters at the end of the string, not just one:
      要注意,Python的strip()与Perl的chomp不同,strip()会去除字符串末尾的所有特殊字符,不止一个:

    • >>> “Hello\n\n\n”.rstrip(”\n")
      `“Hello”

译(二十六)-Python怎么去除末尾的换行符?相关推荐

  1. 二十六. Python基础(26)--类的内置特殊属性和方法

    二十六. Python基础(26)--类的内置特殊属性和方法 ● 知识框架 ● 类的内置方法/魔法方法案例1: 单例设计模式 # 类的魔法方法 # 案例1: 单例设计模式 class Teacher: ...

  2. OpenCV学习笔记(二十六)——小试SVM算法ml OpenCV学习笔记(二十七)——基于级联分类器的目标检测objdect OpenCV学习笔记(二十八)——光流法对运动目标跟踪Video Ope

    OpenCV学习笔记(二十六)--小试SVM算法ml 总感觉自己停留在码农的初级阶段,要想更上一层,就得静下心来,好好研究一下算法的东西.OpenCV作为一个计算机视觉的开源库,肯定不会只停留在数字图 ...

  3. GPS从入门到放弃(二十六) --- RTKLIB函数解析

    GPS从入门到放弃(二十六) - RTKLIB函数解析 为了贴合这个系列的标题"从入门到放弃",在入门之后现在就要放弃此方向了.虽然感觉遗憾,暂时也没有办法.在此附上此系列最后一篇 ...

  4. (转)tensorflow入门教程(二十六)人脸识别(上)

    https://blog.csdn.net/rookie_wei/article/details/81676177 1.概述 查看全文 http://www.taodudu.cc/news/show- ...

  5. LINUX学习基础篇(二十六)swap分区

    LINUX学习基础篇(二十六)swap分区 swap分区 查看swap分区大小 分配swap分区 格式化swap分区 增加swap分区 swap分区 Linux中的swap分区相当于Windows中的 ...

  6. 电脑高手应用技巧荟萃(电脑知识二十六)

    电脑高手应用技巧荟萃(电脑知识二十六) 2010年12月07日 ★"锁定计算机"快捷方式 我想在桌面上创建一个"锁定计算机"的快捷方式,请问应该如何实现呢? 首 ...

  7. 2021年大数据Hadoop(二十六):YARN三大组件介绍

    全网最详细的Hadoop文章系列,强烈建议收藏加关注! 后面更新文章都会列出历史文章目录,帮助大家回顾知识重点. 目录 本系列历史文章 前言 Yarn三大组件介绍 ResourceManager No ...

  8. 模板方法模式 Template method 行为型 设计模式(二十六)

    模板方法模式 Template method 上图为网上百度的一份简历模板截图 相信大家都有求职的经历,那么必然需要简历,写简历的时候,很可能你会网上检索一份简历模板,使用此模板的格式,然后替换为你的 ...

  9. 窗口消息——Windows核心编程学习手札之二十六

    窗口消息 --Windows核心编程学习手札之二十六 Windows允许一个进程至多建立10000个不同类型的用户对象(user object):图符.光标.窗口类.菜单.加速键表等,当一个线程调用一 ...

最新文章

  1. 用 jQuery Masonry 插件创建瀑布流式的页面(转)
  2. 分层设计与领域设计融合架构设计
  3. python b站 礼物_[我叫以赏]Python获取B站UP主粉丝数
  4. 我在阿里招前端,我该怎么帮你?
  5. 饭卡可以用水冲洗吗_薄壁不锈钢水管真的可以满足大众用水健康管道的要求吗?...
  6. 解析单句sql_SqlParser 一个利用正则表达式解析单句SQL的类
  7. 七牛云 转码_开发者选择短视频SDK,为何青睐七牛云?
  8. 增强 扫描王 源码_BlueScan:一款功能强大的蓝牙扫描器
  9. ps读写ddr3里面的数据 zynq_ZYNQ应该如何让PS端的opencv程序读取到ddr中的图像数据...
  10. C语言习题答案【3】(仅参考)
  11. linux测试硬盘读写速度
  12. android 应用升级,系统做了什么?
  13. SQL刷题:排名的问题
  14. android 投屏与控制,android投屏pc及电脑adb控制手机
  15. RB-PEG2000-Pyrene,含有芘丁酸和罗丹明的PEG,Pyrene-PEG2000-Rhodamine
  16. 《Flask Web开发:基于Python的Web应用开发实战》笔记(原创)
  17. 为什么将机器学习应用于资产定价?
  18. Xtext语言语法介绍
  19. 一款网页电路仿真软件
  20. C/C++数据结构课程设计安排

热门文章

  1. 题目:什么是内联函数
  2. Vue3核心源码解析第十一课 AST生成代码
  3. 从自己挖的坑里爬出来
  4. 空洞卷积感受野大小计算
  5. oracle创建存储过程,hibernate调用
  6. uipath锚点的使用
  7. 【设计模式】Builder模式
  8. TSC打印机打印条形码和二维码,java实现方式
  9. 基于android的互动健身平台,基于Android的健身助手APP设计与实现
  10. 面部AR遥控器:使用AR制作动画