Python实现把回车符\r\n转换成\n


Posted in Python onApril 23, 2015

最近在做cocos2d-x的简明配置,发现有的朋友的文本编辑器,自动将\r\n截断成\n,(在unix上换行使用\n,windows上,换行使用的是\r\n)于是,写了这个脚本,希望对一些朋友有所帮助,不用一行一行去改

import os

def replace(filePath, w2u):
  try:
    oldfile = open(filePath, "rb+")     #这里必须用b打开
    path, name = os.path.split(filePath)
    newfile = open(path + '$' + name, "ba+")
    
    old = b''
    new = b''
    if w2u == True:
      old = b'\r'
      new = b''
    else:
      old = b'\n'
      new = b'\r\n'

    data = b''
    while (True):
      data = oldfile.read(200)
      newData = data.replace(old, new)
      newfile.write(newData)
      if len(data) < 200:
        break
    newfile.close()
    oldfile.close()
    
    os.remove(filePath)
    os.rename(path + '$' + name, filePath)
  except IOError as e:
    print(e)
    
if __name__ == "__main__":
  print("请输入文件路径:")
  filePath = input()
  replace(filePath, False)  #这个改为True就可以实现\n变成\r\n

要注意的是,在python里,像\r\n这样的符号,如果是文本打开的话,是找不到\r\n的,而只能找到'\n',所以必须用b(二进制)模式打开。

Python 相关文章推荐
python抓取网页时字符集转换问题处理方案分享
Jun 19 Python
Python编程实现粒子群算法(PSO)详解
Nov 13 Python
Python机器学习算法之k均值聚类(k-means)
Feb 23 Python
matplotlib实现热成像图colorbar和极坐标图的方法
Dec 13 Python
Python之数据序列化(json、pickle、shelve)详解
Aug 30 Python
Python 经典算法100及解析(小结)
Sep 13 Python
关于Python 中的时间处理包datetime和arrow的方法详解
Mar 19 Python
TensorFlow使用Graph的基本操作的实现
Apr 22 Python
Python爬取阿拉丁统计信息过程图解
May 12 Python
python 实现两个npy档案合并
Jul 01 Python
python开发入门——set的使用
Sep 03 Python
Python用户自定义异常的实现
Dec 25 Python
Python实现对比不同字体中的同一字符的显示效果
Apr 23 #Python
Python3里的super()和__class__使用介绍
Apr 23 #Python
Python实现的飞速中文网小说下载脚本
Apr 23 #Python
Python中使用PyQt把网页转换成PDF操作代码实例
Apr 23 #Python
Python里disconnect UDP套接字的方法
Apr 23 #Python
Python实现的Google IP 可用性检测脚本
Apr 23 #Python
Python3.2中的字符串函数学习总结
Apr 23 #Python
You might like
laravel返回统一格式错误码问题
2019/11/04 PHP
jquery中dom操作和事件的实例学习 下拉框应用
2011/12/01 Javascript
javascript读写XML实现广告轮换(兼容IE、FF)
2013/08/09 Javascript
js完美的div拖拽实例代码
2014/01/22 Javascript
5款JavaScript代码压缩工具推荐
2014/07/07 Javascript
PHPMyAdmin导入时提示文件大小超出PHP限制的解决方法
2015/03/30 Javascript
jQuery增加自定义函数的方法
2015/07/18 Javascript
jquery 实现输入邮箱时自动补全下拉提示功能
2015/10/04 Javascript
JavaScript给input的value赋值引发的关于基本类型值和引用类型值问题
2015/12/07 Javascript
node-http-proxy修改响应结果实例代码
2016/06/06 Javascript
JS 设置Cookie 有效期 检测cookie
2017/06/15 Javascript
vue.js组件之间传递数据的方法
2017/07/10 Javascript
webpack里使用jquery.mCustomScrollbar插件的方法
2018/05/30 jQuery
浅谈vue方法内的方法使用this的问题
2018/09/15 Javascript
vue 的点击事件获取当前点击的元素方法
2018/09/15 Javascript
详解Ant Design of React的安装和使用方法
2018/12/27 Javascript
vue中jsonp插件的使用方法示例
2020/09/10 Javascript
python 图片验证码代码分享
2012/07/04 Python
Python数据可视化正态分布简单分析及实现代码
2017/12/04 Python
python实现教务管理系统
2018/03/12 Python
Python补齐字符串长度的实例
2018/11/15 Python
Python实现数据结构线性链表(单链表)算法示例
2019/05/04 Python
Django中在xadmin中集成DjangoUeditor过程详解
2019/07/24 Python
pytorch多进程加速及代码优化方法
2019/08/19 Python
解决numpy矩阵相减出现的负值自动转正值的问题
2020/06/03 Python
python中如何使用虚拟环境
2020/10/14 Python
python re模块常见用法例举
2021/03/01 Python
Python  Asyncio模块实现的生产消费者模型的方法
2021/03/01 Python
解决margin 外边距合并问题
2019/07/03 HTML / CSS
阿里云:Aliyun.com
2017/02/15 全球购物
俄罗斯家居用品购物网站:Евродом
2020/11/21 全球购物
营销主管自我评价怎么写
2013/09/19 职场文书
幼师求职自荐信
2014/05/31 职场文书
孝敬父母的活动方案
2014/08/28 职场文书
2014年护士长工作总结
2014/11/11 职场文书
食品质检员岗位职责
2015/04/08 职场文书