感谢评论区 baiyu33 这位老哥开源的代码转换工具,我这里问题处理的太简单粗暴了,哈哈。放下地址,好用给个star哈-
https://github.com/zchrissirhcz/smart_code_copier
功能:
- tab转空格
- gb2312编码转utf-8
- 去掉行尾空白字符
- 统一单个文件内换行符
- 单个文件的转换
- 整个目录的转换
- 指定
\t对齐到的空格长度
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
今天github上下载了一个python脚本,一运行报 IndentationError: unindent does not match any outer indentation level,
看错误,顾名思义,代码没有对齐,然后使用notepat++打开脚本,再选择视图-->显示符号--->显示空格和制表符.

所有空格位置都被制表符填充了,现在要做的就是批量替换回来,方法很简单,使用replace方法匹配\t即可。
例: " HelloWorld".replace("\t"," ");
用python写个小脚本吧。代码如下 。
#为什么不用中文输出日志呢,因为会乱码,为什么不处理呢,因为我懒。
#-*- coding:utf-8 -*-
import sys
import os
if __name__ == '__main__':
#sys.argv[0]这个参数是脚本名称.
try:
sourceFilePath = sys.argv[1]; #参数1
outFilePath = sys.argv[2]; #参数2
ifExists = os.path.exists(sourceFilePath)
if not ifExists:
print "Error:The Source file not exists!~-";
sys.exit();
with open(sourceFilePath , "r") as f:
sourceFileStr = f.read();
#四个空格
outFileStr = sourceFileStr.replace("\t"," ");
with open(outFilePath,"a+") as w:
w.write(outFileStr);
print "replace success! , output path : " + outFilePath;
except Exception as e:
print e;
print "Error:get args error, ex: python xxx.py sourceFile outFile .....";
听说python加分号会被当做异端给烧死- /怕
用法如下:
python replace.py 源文件 (空格) 输出文件

替换后效果

*:话说windows下截图工具好难用,百度了下有一个叫Snipaste的,用了下确实不错,关键还是免费的,推荐下。
官网在: https://zh.snipaste.com/download.html
嗯,完
当遇到Python的IndentationError时,问题可能出在制表符(TAB)未正确对齐。可以使用工具或编写小脚本来批量将制表符替换为4个空格,例如使用github上的smart_code_copier工具,或者通过Python脚本实现。此错误提示表明代码的缩进不符合Python的语法规范。
5708





