autopep8
autopep8 would auto-format your python script. not only the code indentation, but also other coding spacing styles. It makes your python script to conform PEP8 Style Guide.
pip install autopep8
autopep8 your_script.py # dry-run, only print
autopep8 -i your_script.py # replace content
Update:
Many editors have pep8 plugins that automatically reformat your code right after you save the file. py-autopep8 in emacs
yapf
yapf is a new and better python code formatter. which tries to get the best formatting, not just to conform the guidelines. The usage is quite the same as autopep8.
pip install yapf
yapf your_script.py # dry-run, only print
yapf -i your_script.py # replace content
For more information, like formatting configurations, please read the README.rst on yapf github
Update 2:
Black
Black is much better than yapf. It's smarter and fits most complex formatting cases.
Answer from d2207197 on Stack Overflowautopep8
autopep8 would auto-format your python script. not only the code indentation, but also other coding spacing styles. It makes your python script to conform PEP8 Style Guide.
pip install autopep8
autopep8 your_script.py # dry-run, only print
autopep8 -i your_script.py # replace content
Update:
Many editors have pep8 plugins that automatically reformat your code right after you save the file. py-autopep8 in emacs
yapf
yapf is a new and better python code formatter. which tries to get the best formatting, not just to conform the guidelines. The usage is quite the same as autopep8.
pip install yapf
yapf your_script.py # dry-run, only print
yapf -i your_script.py # replace content
For more information, like formatting configurations, please read the README.rst on yapf github
Update 2:
Black
Black is much better than yapf. It's smarter and fits most complex formatting cases.
Edit: Nowadays, I would recommend autopep8, since it not only corrects indentation problems but also (at your discretion) makes code conform to many other PEP8 guidelines.
Use reindent.py. It should come with the standard distribution of Python, though on Ubuntu you need to install the python2.6-examples package.
You can also find it on the web.
This script attempts to convert any python script to conform with the 4-space standard.
formatting - How do I format text from a file in Python? - Stack Overflow
Is there a format for writing a python script?
Which code formatter do you use?
Ruff format, new tool to format python files that is 30-120x faster than other tools(e.g black or yapf) - benchmark
Videos
Something like this might work:
def print_table(lines, col_num, col_width):
for line_ix in range(0, len(lines), col_num):
print ' -- '.join([line.strip().ljust(col_width) for line in lines[line_ix:line_ix+col_num]])
you could try to use the file type .dat, and then f = loadtxt("file.dat") and that will put your data into an array, using the columns and rows you already have created in the file. You would have to edit the data file a bit, but it certainly would work. and if you wanted to change things even more you could use commands like this,
c = f[:,5] which will create a new array of 1 column, with all the data from column 5 from your original file
I am currently learning Python in high school. When I installed Spyder to step up the IDE I was previously using (IDLE), there was some pre-written text. Something like this-
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on %(date)s
@author: %(username)s
"""
This made me wonder if there is a properly defined set of 'etiquettes' (if I may) of writing a python script. For starters, I think that you usually write all import statements at the very start and define all the user-built functions after import statements. I want to know if there are any more like these? If you could include a python script of one of your projects (through pastebin or whatever) which may include these etiquettes, that would be awesome.
Also, Would appreciate it if someone could help me out understanding the default Spyder template up there.