I am new to Python and have written a code in notepad++. I have used spaces instead of tab and I guess I have used the correct indentation format. But I still get thie error. I dont understand what am I doing wrong. Here is the code,
#!/usr/bin/python
import sys
import shutil
import sys
def Usage() :
print "LabelFile TranscriptionFile PhonemeFile"
def main(argv = None) :
if len(sys.argv) !=3 :
Usage()
else :
LabelFile = sys.argv[1]
TranscriptionFile = sys.argv[2]
PhonemeFile = sys.argv[3]
if (os.path.exists(LabelFile)) :
InFile = open(LabelFile, "r")
TFile = open(TranscriptionFile, "w")
PFile = open(PhonemeFile, "w")
for line in iter(InFile) :
list = line.split()
Tlist = list.pop(3)
Plist = list[2]
TFile.write(" ".join(list) + "\n")
PFile.write("".join(list) + " ")
InFile.close()
TFile.close()
PFile.close()
if __name__ == "__main__" :
sys.exit(main())
开发者_StackOverflow社区
Please help. Thank you very much.
There is an indentation too much in front of
for line in iter(InFile) :
Reduce the indentation for that line and the following eight lines, and you should be all set.
line 21:
for line in iter(InFile) :
start aligning it with the line above
Tracebacks also tell you error line number so it's quite easy to fix if you read 'em all
Moreover it's a good habit to indent 4 spaces and not 2:
#!/usr/bin/python
import sys
import shutil
import sys
def Usage() :
print "LabelFile TranscriptionFile PhonemeFile"
def main(argv = None) :
if len(sys.argv) !=3 :
Usage()
else :
LabelFile = sys.argv[1]
TranscriptionFile = sys.argv[2]
PhonemeFile = sys.argv[3]
if (os.path.exists(LabelFile)) :
InFile = open(LabelFile, "r")
TFile = open(TranscriptionFile, "w")
PFile = open(PhonemeFile, "w")
for line in iter(InFile) :
list = line.split()
Tlist = list.pop(3)
Plist = list[2]
TFile.write(" ".join(list) + "\n")
PFile.write("".join(list) + " ")
InFile.close()
TFile.close()
PFile.close()
if __name__ == "__main__" :
sys.exit(main())
First off. Although it's not a requirement, it's commonplace using four spaces to indent, not two (unless, of course, you are extending some code that already has 2 spaces indentations)
Then, your amended code (there was an indentation error on the for
loop on line 21):
#!/usr/bin/python
import sys
import shutil
import sys
def Usage() :
print "LabelFile TranscriptionFile PhonemeFile"
def main(argv = None) :
if len(sys.argv) !=3 :
Usage()
else :
LabelFile = sys.argv[1]
TranscriptionFile = sys.argv[2]
PhonemeFile = sys.argv[3]
if (os.path.exists(LabelFile)) :
InFile = open(LabelFile, "r")
TFile = open(TranscriptionFile, "w")
PFile = open(PhonemeFile, "w")
for line in iter(InFile) :
list = line.split()
Tlist = list.pop(3)
Plist = list[2]
TFile.write(" ".join(list) + "\n")
PFile.write("".join(list) + " ")
InFile.close()
TFile.close()
PFile.close()
if __name__ == "__main__" :
sys.exit(main())
With notepad++, in "settings/preferences" menu, "Edit components" panel, there is an option Tab size, use 4, and a checkbox "replace by space" Check it and you will avoid mixing of tabs and spaces.
精彩评论