I am trying to read a tab-separated file and collect all characters except control characters. If a control character is hit, the remainder of line should be ignored too. I've tried the following code in Python 3.5, using a for..else
loop:
import curses.ascii
input_file = ...
chars = set()
with open(input_file) as file:
for line in file.readlines():
source, target = line.split("\t")
for c in source.strip() + target.strip():
if curses.ascii.iscntrl(c):
print("Control char hit.")
break
chars.add(c)
else:
print("Line contains control character:\n" + line)
continue
print("Line contains no control character:\n" + line.strip())
I'd expect this to check each character for being a control character and if it hits one (break
is triggered), skip to the next line, hence trigger the else
/continue
statement.
What happens instead is that continue
is always triggered, even if the break
statement in the if
clause is never reached for a line. Consequently, the final print
statement is never reached either.
What am I doing wrong?
Aucun commentaire:
Enregistrer un commentaire