在 windows 和 linux 系统,换行符有时需要转换,其代码文件 HxUntils.py 如下:

 ''' HxUtils.py 2018 by x01 '''

import os, sys

def convert_endlines(filepath, tounix=True):
try:
oldfile = open(filepath, 'rb+')
path, name = os.path.split(filepath)
newfile = open(path + '$' + name, 'ba+') old = b'\r'
new = b''
if not tounix:
old = b'\n'
new = b'\r\n' while True:
olddata = oldfile.read(1024)
newdata = olddata.replace(old, new)
newfile.write(newdata)
if len(olddata) < 1024:
break newfile.close()
oldfile.close() os.remove(filepath)
os.rename(path + '$' + name, filepath) except IOError as e:
print(e) def convert_endlines_for_dir():
dirpath, tounix, extname = ('.', True, '.py')
if len(sys.argv) == 2:
dirpath = sys.argv[1]
elif len(sys.argv) == 3:
dirpath, tounix = (sys.argv[1], sys.argv[2])
elif len(sys.argv) == 4:
dirpath, tounix, extname = (sys.argv[1], sys.argv[2], sys.argv[3]) for (thisdir, subshere, fileshere) in os.walk(dirpath):
for filename in fileshere:
if filename.endswith(extname):
#print(os.path.join(thisdir,filename))
convert_endlines(os.path.join(thisdir,filename), tounix) if __name__ == '__main__':
convert_endlines_for_dir()

使用方法如下:

$ python3 HxUntils.py  [dirpath tounix extname]

python2 的 print 命令替换为 python3 的 print() 函数:

def convert_print(dirpath='.'):
'''
转换 python2 中的 print 命令到 python3
'''
if len(sys.argv) == 2:
dirpath = sys.argv[1] for (thisdir, subshere, fileshere) in os.walk(dirpath):
for filename in fileshere:
if filename.endswith('.py'):
print2to3(os.path.join(thisdir,filename)) def print2to3(filename):
fr = open(filename)
tempfile = open(filename +'.tmp', 'a+')
for line in fr:
tl = replace_line(line)
tempfile.write(tl)
fr.close()
tempfile.close() os.remove(filename)
os.rename(filename+'.tmp', filename) def replace_line(line):
if 'print(' in line:
return line tl = ''
pattern = r'(".*?;.*?")|(\'.*?;.*?\')'
result = re.search(pattern, line)
if result and ('print ' in line):
line = line.replace('print ', 'print(')
line = line[:len(line)-1] + ')' + line[len(line)-1:]
return line if ('print' in line) and (';' not in line):
index = line.index('print')
tl += line[:index+5] + '(' + line[index+6:len(line)-1] + ')' + line[len(line)-1:] # endline \n
elif ('print' in line) and (';' in line):
i1 = line.index('print')
i2 = line.index(';')
tl += line[:i1+5] + '(' + line[i1+6:i2] + ')' + line[i2:]
if 'print' in line[i2:]:
tl += replace_line(line[i2:])
else:
tl = line return tl

借用 lib2to3 如下,添加 convert_2to3() 到 HxUtils.py 中:

def convert_2to3():
''' usage: python3 HxUtils.py -w [dirpath] '''
import sys
from lib2to3.main import main sys.exit(main("lib2to3.fixes"))

HxUtils: 批量转换换行符,print2to3的更多相关文章

  1. excel 批量替换换行符

    在excel批量替换换行符操作步骤: 全选需要查找换行符的范围 CTRL+H调出查找和替换 在查找内容内输入"ctrl+enter"两个组合键 点击查找全部即可. 在excel中输 ...

  2. Excel批量删除换行符_clean函数

    http://jingyan.baidu.com/article/e2284b2b489b96e2e6118d30.html CLEAN函数,用于删除文本中不能打印的字符.对从其他应用程序中输入的文本 ...

  3. 禁止 git 自动转换换行符

    开发团队都在 windows 下开发,有IDE管理代码.对我们来说,最好是禁用换行转换符的功能.我用 cygwin 提交代码,提交时总提示自动转换换符.其实都不用提交,仅运行 git status 看 ...

  4. git换行符自动转换导致整个文件被修改的解决方案

    不少开发者可能遇到过这个问题:从git上拉取服务端代码,然后只修改了一处地方,准备提交时,用diff软件查看,却发现整个文件都被修改了.这是git自动转换换行符导致的问题. 原因 不同操作系统使用的换 ...

  5. 使用Word批量删除换行和空白行

    转载自:https://blog.csdn.net/dearmorning/article/details/78811137 问题一:从pdf文档中复制一部分内容到word的时候,pdf的自动换行会自 ...

  6. git在不同平台windows、linux、mac 上换行符的问题

    0.01 不同平台上换行符的问题 1,不同平台对换行符的制定不同 windows <回车换行> (carriage return AND line feed) “\n\r” CRLF Un ...

  7. Linux换行符和Windows换行符的区别与转换

    不同系统文本文件的行尾换行符不同:    Windows为一个回车'\r'(CR或^M)和一个换行'\n'(NL或LF)(括号内是其它显示方法)    Linux为一个换行'\n'    Mac为一个 ...

  8. SQL:指定名称查不到数据的衍伸~空格 换行符 回车符的批量处理

    异常处理汇总-数据库系列  http://www.cnblogs.com/dunitian/p/4522990.html 先看看啥情况 复制查询到的数据,粘贴一下看看啥情况 那就批量处理一下~ 就这样 ...

  9. git 换行符LF与CRLF转换问题

    git 换行符LF与CRLF转换问题 一.背景 在各操作系统下,文本文件所使用的换行符是不一样的.UNIX/Linux 使用的是 0x0A(LF),早期的 Mac OS 使用的是0x0D(CR),后来 ...

随机推荐

  1. 沉淀再出发:IoC和AOP的本质

    沉淀再出发:IoC和AOP的本质 一.前言 关于IoC和AOP这两个概念,如果我们没有深入的理解可以说是根本就不理解Spring这个架构的,同样的由Spring演变出来的Spring Boot和Spr ...

  2. SpringBoot 中解决跨域请求

    CORS 理解 同源策略是web浏览器实现的一个重要的安全概念,它防止JavaScript代码对不同的来源(例如,不同的域)发出请求,而不是它所服务的来源.虽然同源策略有效地防止来自不同来源的资源,但 ...

  3. SVN There are unfinished transactions detected

    在ECLIPSE中报这个错,不能提交和更新代码和clean up 解决办法:关闭ECLIPSE,使用工具对SVN执行 clean up. 重新启动ECLIPSE,解决冲突文件,可以正常使用SVN

  4. java查找字符串里与指定字符串相同的个数

    public class EmployeeDemo { //方法一: public int search(String str,String strRes) {//查找字符串里与指定字符串相同的个数 ...

  5. 为什么 Category 不能增加成员变量-nonfragile

    三.既然是 non-fragile ivars,为什么 Category 不能增加成员变量?     看过一些资料,理由并不是很让人信服.我觉得并不是做不到,只是现在没有做,现在不支持.我在 Opti ...

  6. BZOJ1861:[ZJOI2006]书架(Splay)

    Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下 ...

  7. C/C++ 格式化读取和读取一行

    文件内容 23 21 4 1 1 0 114 1 1 1 8 112 5 0 0 0 114 1 0 0 0 115 52 4 1 0 1 134 4 0 1 12 131 4 1 1 0 133 5 ...

  8. Vue Spa切换页面时更改标题

    在Vue组件化开发过程中,因为是单页面开发,但是有时候需要页面的title根据情况改变,于是上网查了一下,各种说法花(wo)里(kan)胡(bu)哨(dong), 于是想到一个黑科技 documet. ...

  9. Level/levelup-1-简介

    https://github.com/Level/levelup A node.js wrapper for abstract-leveldown compliant stores 一个为实现抽象le ...

  10. windows环境安装docker,并下载lamp镜像

    1.PC系统:windows10专业版 2.开启Hyper-V 此电脑->右击->属性->控制面板主页->(查看方式为小图标)程序和功能->右上方启动或关闭windows ...