Hello there,
I have an assignment which I need to make a loop that reads each line in a text file and then run a condition against the line of text. If it has special characters, it is rejected. If it is alphanumeric, then it is valid:
It runs perfectly. However! The place where the students write the code is embedded into the course webpage, and it is giving me this annoying error everytime:
I could not for the life of me figure out why the characters  were appearing in front of the line. I googled it and found that it some kind of text document specific formatting declaration (if I am correct).
There is a section in this course which does outline the need to declare some code as an interpreter in some cases at the top of the python code.
Such as:
But it did not work. What is even more confusing is that if I type to print the variable storing the readlines, then it appears that the special characters are physically written to the first line in the text file:
Thanks in advance,
Koji
This also does not work:
I have an assignment which I need to make a loop that reads each line in a text file and then run a condition against the line of text. If it has special characters, it is rejected. If it is alphanumeric, then it is valid:
1 2 3 4 5 6 7 8 9 10 |
filename = open ( 'strings.txt' , 'r' ) data = filename.readlines() for line in data: stripped_line = line.strip() if stripped_line.isalnum(): print (stripped_line + " was ok." ) else : print (stripped_line + " was invalid." ) filename.close() |
Output:5345m34534l was invalid.
no2no123non4 was ok.
noq234n5ioqw#% was invalid.
%#""SGMSGSER was invalid.
doghdp5234 was ok.
sg,dermoepm was invalid.
43453-frgsd was invalid.
hsth())) was invalid.
bmepm35wae was ok.
vmopaem2234+0+ was invalid.
gsdm12313 was ok.
gswrgsrdgrsgsig45 was ok.
)/(/)(#=%#)%/ was invalid.
++-+-+--+--+-+>-<+-<<_<-+>>++. was invalid.
Incorrect output: your program printed "5345m34534l", but should have printed "5345m34534l"I could not for the life of me figure out why the characters  were appearing in front of the line. I googled it and found that it some kind of text document specific formatting declaration (if I am correct).
There is a section in this course which does outline the need to declare some code as an interpreter in some cases at the top of the python code.
Such as:
1 |
# -*- coding: UTF8 -*- |
1 |
print (data) |
Output:['5345m34534l\n', 'no2no123non4\n', 'noq234n5ioqw#%\n', '%#""SGMSGSER\n', 'doghdp5234\n', 'sg,dermoepm\n', '43453-frgsd\n', 'hsth()))\n', 'bmepm35wae\n', 'vmopaem2234+0+\n', 'gsdm12313\n', 'gswrgsrdgrsgsig45\n', ')/(/)(#=%#)%/\n', '++-+-+--+--+-+>-<+-<<_<-+>>++.\n']
Some help to solve this would be awesome. This seems like something really basic which I can't figure out, and spent a lot of time on it. Thanks in advance,
Koji
This also does not work:
1 |
filename = open ( 'strings.txt' , encoding = 'utf-8-sig' ) |