不用缩进的python
2016-06-15 09:23
453 查看
初学python多次,都被python的缩进搞得头疼,还是习惯c的风格,查了查好像没人弄一个c风格的python,只能自己想办法了。
解决办法很简单,就是给程序块加上大括号,方式如下:
下面是python原来的格式代码:
运行上面第二个代码,输入第一个代码文件名(以".pyi"为扩展名),执行成功后会生成同名的扩展名为".py"的文件,代码符合python缩进要求。
这样再也不用发愁缩进的问题了。
解决办法很简单,就是给程序块加上大括号,方式如下:
#!/usr/bin/python #coding:utf-8 import string infilename = raw_input('enter file name:') index = infilename.find('.pyi') if index != -1: { outfilename = infilename[:index] outfilename += '.py' } else: { print 'Wrong file type!' exit() } count = 0 #with open(infilename,'r') as fin #with open(outfilename,'w') as fout fin = open(infilename,'r') fout=open(outfilename,'w') for line in fin.readlines(): { line = line.strip() if line.startswith('{'): { count += 1 continue } elif line.startswith('}'): { count -= 1 if count < 0: { print 'find a unmatch }' exit() } continue } else: { x=count print x space = '' while x > 0: { space += ' ' x -= 1 } fout.write(space + line + '\n') } } if count > 0: { print 'find a unmatch {' } fin.close() fout.close()
下面是python原来的格式代码:
#!/usr/bin/python #coding:utf-8 import string infilename = raw_input('enter file name:') index = infilename.find('.pyi') if index != -1: outfilename = infilename[:index] outfilename += '.py' else: print 'Wrong file type!' exit() count = 0 #with open(infilename,'r') as fin #with open(outfilename,'w') as fout fin = open(infilename,'r') fout=open(outfilename,'w') for line in fin.readlines(): line = line.strip() if line.startswith('{'): count += 1 continue elif line.startswith('}'): count -= 1 if count < 0: print 'find a unmatch }' exit() continue else: x=count space = '' while x > 0: space += ' ' x -= 1 fout.write(space + line + '\n') if count > 0: print 'find a unmatch {' fin.close() fout.close()为了区别,把加括号的代码扩展名定为".pyi"。
运行上面第二个代码,输入第一个代码文件名(以".pyi"为扩展名),执行成功后会生成同名的扩展名为".py"的文件,代码符合python缩进要求。
这样再也不用发愁缩进的问题了。
相关文章推荐
- Python3 函数式编程
- 地铁译:Spark for python developers ---Spark流式数据处理
- Python编码整理
- python自定义日志函数测试
- Python3 函数
- Python3 列表
- python的2.5与2.7版本中ftp模块的一个小区别
- Python ftplib模块
- python常用技巧
- Python NLP相关工具
- 一个最简单的博客园文章密码暴力破解器-python3实现
- python实现概率分布
- Python \x invalid escape error 和 windows 下的文件路径使用问题
- 安装MYSQL-PYTHON包报错mysql_config not found解决办法
- Python学习笔记(五)
- [leetcode]53. Maximum Subarray 最大连续子串python实现【medium】
- python 读取全国城市aqi数据,差值生成png图片
- python3.5仿微软计算器程序
- 使用python3.5仿微软记事本notepad
- python3音乐播放器简单实现代码