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判断端口是否打开的实现代码
Feb 10 Python
python进程管理工具supervisor使用实例
Sep 17 Python
浅谈python中截取字符函数strip,lstrip,rstrip
Jul 17 Python
python实现文本文件合并
Dec 29 Python
python实现简单聊天应用 python群聊和点对点均实现
Sep 14 Python
Python SQLite3简介
Feb 22 Python
django-allauth入门学习和使用详解
Jul 03 Python
python区块及区块链的开发详解
Jul 03 Python
基于python实现地址和经纬度转换
May 19 Python
如何使用python自带IDLE的几种方法
Oct 10 Python
Python3.9新特性详解
Oct 10 Python
Opencv实现二维直方图的计算及绘制
Jul 21 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
php 静态页面中显示动态内容
2009/08/14 PHP
PHP OPCode缓存 APC详细介绍
2010/10/12 PHP
分享8个最佳的代码片段在线测试网站
2013/06/29 PHP
解析php防止form重复提交的方法
2013/07/01 PHP
php5.5中类级别的常量使用介绍
2013/10/02 PHP
php检测url是否存在的方法
2015/04/14 PHP
防止网站内容被拷贝的一些方法与优缺点好处与坏处分析
2007/11/30 Javascript
用showModalDialog弹出页面后,提交表单总是弹出一个新窗口
2009/07/18 Javascript
JavaScript 序列化对象实现代码
2009/12/18 Javascript
为Extjs加加速(javascript加速)
2010/08/19 Javascript
Node.js中的process.nextTick使用实例
2015/06/25 Javascript
JavaScript事件学习小结(一)事件流
2016/06/09 Javascript
总结javascript中的六种迭代器
2016/08/16 Javascript
iOS和Android用同一个二维码实现跳转下载链接的方法
2016/09/28 Javascript
jQuery Form表单取值的方法
2017/01/11 Javascript
jQuery ajax读取本地json文件的实例
2017/10/31 jQuery
如何在JavaScript中创建具有多个空格的字符串?
2020/02/23 Javascript
js根据后缀判断文件文件类型的代码
2020/05/09 Javascript
Vue3.0的优化总结
2020/10/16 Javascript
vue+vant 上传图片需要注意的地方
2021/01/03 Vue.js
[00:17]游戏风云独家报道:DD赛后说出数字秘密 吓死你们啊!
2014/07/13 DOTA
Python3基础之list列表实例解析
2014/08/13 Python
使用Nginx+uWsgi实现Python的Django框架站点动静分离
2016/03/21 Python
使用Python的Django框架结合jQuery实现AJAX购物车页面
2016/04/11 Python
结合Python的SimpleHTTPServer源码来解析socket通信
2016/06/27 Python
解决Python plt.savefig 保存图片时一片空白的问题
2019/01/10 Python
python中logging模块的一些简单用法的使用
2019/02/22 Python
pandas DataFrame 警告(SettingWithCopyWarning)的解决
2019/07/23 Python
python通过对字典的排序,对json字段进行排序的实例
2020/02/27 Python
pycharm实现在子类中添加一个父类没有的属性
2020/03/12 Python
Python命令行参数argv和argparse该如何使用
2021/02/08 Python
Python与C/C++的相互调用案例
2021/03/04 Python
怎样在 Applet 中建立自己的菜单(MenuBar/Menu)?
2012/06/20 面试题
2014年技术工作总结范文
2014/11/20 职场文书
护士求职自荐信范文
2015/03/04 职场文书
Python基础之变量的相关知识总结
2021/06/23 Python