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+django实现简单的文件上传
Aug 17 Python
利用Python获取操作系统信息实例
Sep 02 Python
Python 专题四 文件基础知识
Mar 20 Python
使用pycharm生成代码模板的实例
May 23 Python
python xpath获取页面注释的方法
Jan 14 Python
Python定时任务工具之APScheduler使用方式
Jul 24 Python
Python 识别12306图片验证码物品的实现示例
Jan 20 Python
python json load json 数据后出现乱序的解决方案
Feb 27 Python
基于pycharm实现批量修改变量名
Jun 02 Python
Python操作dict时避免出现KeyError的几种解决方法
Sep 20 Python
Python urllib3软件包的使用说明
Nov 18 Python
python 实现百度网盘非会员上传超过500个文件的方法
Jan 07 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中cookie的使用方法
2014/03/29 PHP
php生成QRcode实例
2014/09/22 PHP
php获取textarea的值并处理回车换行的方法
2014/10/20 PHP
Juqery Html(),append()等方法的Bug解决方法
2010/12/13 Javascript
JS 控件事件小结
2012/10/31 Javascript
jQuery实现响应浏览器缩放大小并改变背景颜色
2014/10/31 Javascript
jQuery简单实现网页选项卡特效
2014/11/24 Javascript
jQuery中not()方法用法实例
2015/01/06 Javascript
JQuery实现动态添加删除评论的方法
2015/05/18 Javascript
jQuery多级手风琴菜单实例讲解
2015/10/22 Javascript
jquery if条件语句的写法
2016/05/19 Javascript
jquery uploadify如何取消已上传成功文件
2017/02/08 Javascript
vue之a-table中实现清空选中的数据
2019/11/07 Javascript
详细分析React 表单与事件
2020/07/08 Javascript
解决vue prop传值default属性如何使用,为何不生效的问题
2020/09/21 Javascript
[58:42]DOTA2上海特级锦标赛C组败者赛 Newbee VS Archon第一局
2016/02/27 DOTA
python爬取网站数据保存使用的方法
2013/11/20 Python
Android模拟器无法启动,报错:Cannot set up guest memory ‘android_arm’ Invalid argument的解决方法
2016/07/01 Python
Python实现基本数据结构中队列的操作方法示例
2017/12/04 Python
python使用pygame模块实现坦克大战游戏
2020/03/25 Python
在VS2017中用C#调用python脚本的实现
2019/07/31 Python
Pytorch中的VGG实现修改最后一层FC
2020/01/15 Python
python小白切忌乱用表达式
2020/05/29 Python
迷你唐卡软皮鞋:Minnetonka Moccasin
2018/05/01 全球购物
应届中专生自荐书范文
2014/02/13 职场文书
校庆活动方案
2014/03/31 职场文书
婚假请假条怎么写
2014/04/10 职场文书
寒假家长评语大全
2014/04/16 职场文书
商铺门前三包责任书
2014/07/25 职场文书
2014年项目经理工作总结
2014/11/24 职场文书
2014城乡环境综合治理工作总结
2014/12/19 职场文书
优秀员工推荐材料
2014/12/20 职场文书
基层党建工作简报
2015/07/21 职场文书
辅导员学期工作总结
2015/08/14 职场文书
2016年圣诞节寄语(一句话)
2015/12/07 职场文书
详解Node.js如何处理ES6模块
2021/05/15 Javascript