You goofed this part up:
if len(parts) < 1: column2 = parts[1]
I'm pretty sure you don't want to access an element that doesn't exist.
But regardless, you don't want to depend on variables that may or may not exist. Be sure to give them some sort of sane default.
Answer from Ignacio Vazquez-Abrams on Stack OverflowVideos
You goofed this part up:
if len(parts) < 1: column2 = parts[1]
I'm pretty sure you don't want to access an element that doesn't exist.
But regardless, you don't want to depend on variables that may or may not exist. Be sure to give them some sort of sane default.
- You want to swap the first and second columns.
- Your input file, has the same name as your output file, so the first time you run your program you will overwrite your input file. Use differente names.
- If you use the
withstatement, you avoid open and close, and then open and close as you have now. You can do your process with both files open.
If every line in your input file has two columns, something as simple as this would do the trick:
with open ("class1.txt", 'r') as fin, open("class2.txt", 'w') as fout:
for line in fin.readlines():
parts = line.split()
fout.write(parts[1]+' '+parts[0]+'\n']
But if you have some lines with one column and others with more than two, you would have to do something more. How is your file, and how would you need your output?
As your code is not contained in a class or function it is expecting those variables to be constants and as such they should be uppercase.
You can read PEP8 for further information.
EDIT: As others have mentioned, pylint expects that global variables should be UPPERCASE. If the warnings really bother you, you can circumvent them by wrapping small snippets like this in a main()-function and then use the if __name__ == "__main__"-convention. Or if you care, you can modify the regular expressions that pylint uses to validate variable names.
From the developers of Pylint.
In this case Pylint is telling me that those variables appear to be constants and should be all UPPERCASE. This rule is in fact a naming convention that is specific to the folks at Logilab who created Pylint. That is the way they have chosen to name those variables. You too can create your own in-house naming conventions but for the purpose of this tutorial, we want to stick to the PEP-8 standard. In this case, the variables I declared should follow the convention of all lowercase. The appropriate rule would be something like: "should match [a-z_][a-z0-9_]{2,30}$". Notice the lowercase letters in the regular expression (a-z versus A-Z)
You can test it by running:
pylint --const-rgx='[a-z_][a-z0-9_]{2,30}$' x.py
You are missing a close-parenthesis on line 5.
You need to add parentheses on line 5. Compare with this code:
from math import *
a = 6378137.0
c = 6356752.314245
e = sqrt( 1 - ((c**2)/(a**2)))
s = (2 * (pi) * ( a**2 ) * ( 1 + ( ( ( 1 - ( e**2 ) ) / e ))) * (atanh( e )) )
print( s )
In other words, you need to add a " ) " at the end of line 5. I hope that helps. Please let me know if I need to clarify anything.