Tool to convert python indentation from spaces to tabs? [closed]

*爱你&永不变心* 提交于 2019-11-27 06:44:42

问题


I have a some .py files that use spaces for indentation, and I'd like to convert them to tabs.

I could easily hack together something using regexes, but I can think of several edge cases where this approach could fail. Is there a tool that does this by parsing the file and determining the indentation level the same way the python interpreter does?


回答1:


If there are not many files to convert, you can open them in vim, and use the :retab command.

See the vim documentation for more information.




回答2:


Python includes a script for the opposite (tabs to spaces). It's C:\Python24\Tools\Scripts\reindent.py for me




回答3:


:retab will swap tab with spaces, and :retab! will swap spaces with tab. 1 tab = 4 spaces, 4 spaces = 1 tab, depending on your tab setting.




回答4:


If you use Linux, you might also play around with unexpand:

Convert blanks in each FILE to tabs, writing to standard output. With no FILE, or when FILE is -, read standard input.




回答5:


In emacs, M-x tabify will convert spaces to tabs where possible. You'll probably want to set the tab-width variable appropriately.

I don't know if this addresses your concern that spaces be interpreted in the same way as the python interpreter, but you could always load up python-mode and use M-x indent-region.



来源:https://stackoverflow.com/questions/338767/tool-to-convert-python-indentation-from-spaces-to-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!