Checking characters in an input file line, and returning if they contain only valid characters
So I want to run a program that will read in a file line by line, and will then print out either Valid or Invalid based on what each line contains.
For this example, I am saying that the input file line can contain ABCabc or a space. If the line only contains these things, the word Valid should be printed. If it is just white space, or contains any other characters or letters, it should print out “Invalid”.
This is what I have come up with:
I can’t seem to get it to ever print out “Valid”
Can you tell why? Thanks!
input = sys.argv[1]
input = open(input,"r")
correctInput = ‘ABCabc ‘
line1 = input.readline()
while line1 != "":
if all(char in correctInput for char in line1):
print “Valid”
line2 = input.readline()
else:
print “Invalid”
line2 = input.readline()
line1 = line2
python readline
add a comment |
So I want to run a program that will read in a file line by line, and will then print out either Valid or Invalid based on what each line contains.
For this example, I am saying that the input file line can contain ABCabc or a space. If the line only contains these things, the word Valid should be printed. If it is just white space, or contains any other characters or letters, it should print out “Invalid”.
This is what I have come up with:
I can’t seem to get it to ever print out “Valid”
Can you tell why? Thanks!
input = sys.argv[1]
input = open(input,"r")
correctInput = ‘ABCabc ‘
line1 = input.readline()
while line1 != "":
if all(char in correctInput for char in line1):
print “Valid”
line2 = input.readline()
else:
print “Invalid”
line2 = input.readline()
line1 = line2
python readline
3
please don't create a variable calledinput
– Ayxan
Nov 13 '18 at 20:02
add a comment |
So I want to run a program that will read in a file line by line, and will then print out either Valid or Invalid based on what each line contains.
For this example, I am saying that the input file line can contain ABCabc or a space. If the line only contains these things, the word Valid should be printed. If it is just white space, or contains any other characters or letters, it should print out “Invalid”.
This is what I have come up with:
I can’t seem to get it to ever print out “Valid”
Can you tell why? Thanks!
input = sys.argv[1]
input = open(input,"r")
correctInput = ‘ABCabc ‘
line1 = input.readline()
while line1 != "":
if all(char in correctInput for char in line1):
print “Valid”
line2 = input.readline()
else:
print “Invalid”
line2 = input.readline()
line1 = line2
python readline
So I want to run a program that will read in a file line by line, and will then print out either Valid or Invalid based on what each line contains.
For this example, I am saying that the input file line can contain ABCabc or a space. If the line only contains these things, the word Valid should be printed. If it is just white space, or contains any other characters or letters, it should print out “Invalid”.
This is what I have come up with:
I can’t seem to get it to ever print out “Valid”
Can you tell why? Thanks!
input = sys.argv[1]
input = open(input,"r")
correctInput = ‘ABCabc ‘
line1 = input.readline()
while line1 != "":
if all(char in correctInput for char in line1):
print “Valid”
line2 = input.readline()
else:
print “Invalid”
line2 = input.readline()
line1 = line2
python readline
python readline
asked Nov 13 '18 at 19:59
TsangTsang
83
83
3
please don't create a variable calledinput
– Ayxan
Nov 13 '18 at 20:02
add a comment |
3
please don't create a variable calledinput
– Ayxan
Nov 13 '18 at 20:02
3
3
please don't create a variable called
input
– Ayxan
Nov 13 '18 at 20:02
please don't create a variable called
input
– Ayxan
Nov 13 '18 at 20:02
add a comment |
2 Answers
2
active
oldest
votes
If you print out the value of line1
before your if else statement, you'll see it has a newline character in it. (The n
character.) This is the character that gets added to the end of each line whenever you hit the enter key on the keyboard, and you need to either discard the newline characters or include them as valid input.
To include it as valid input
Change correctInput = 'ABCabc '
to
correctInput = 'ABCabc n'
.
Or to discard the newline characters change
if all(char in correctInput for char in line1):
to
if all(char in correctInput for char in line1.replace('n', '')):
Either method will work.
Bytheway, input
is a function in Python. Although you're allowed to use it as a variable name, doing so will prevent you from using the input function in your program. Because of this, it is considered bad practice to use any of the built in function names as your variable names.
RegEx Solution
Just for fun, I came up with the following solution which solves your problem using regular expressions.
import re
with open(sys.argv[1]) as fh:
valid_lines = re.findall('^[ABCabc ]+n', fh.read())
This finds any valid lines using the pattern '^[ABCabc ]+n'
. What does this regular expression pattern do?
- The
^
symbol signifies the start of a line - Then comes the
[ABCabc ]
. When brackets are used, only characters inside of those brackets will be allowed. - The
+
after the brackets means that those characters that where in brackets must be found 1 or more times. - And lastly we make sure the valid characters we found are followed by a newline character (
n
). This ensures we checked the complete line for valid characters.
1
Awesome, this fixed it, thank you! Also good to know about input being a function. I am new to python (obviously) so that is good to know. Thanks again.
– Tsang
Nov 14 '18 at 6:11
add a comment |
Its because readline doesn't remove 'n' from end of the line. You could ignore that problem by splitting whole file content in lines and than validate them one by one.
import sys
file_name = sys.argv[1]
file = open(file_name ,"r")
correctInput = 'ABCabc '
lines = file.read().splitlines()
for line1 in lines:
if all(char in correctInput for char in line1):
print 'Valid'
else:
print 'Invalid'
That makes sense. Thank you
– Tsang
Nov 14 '18 at 6:10
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288601%2fchecking-characters-in-an-input-file-line-and-returning-if-they-contain-only-va%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you print out the value of line1
before your if else statement, you'll see it has a newline character in it. (The n
character.) This is the character that gets added to the end of each line whenever you hit the enter key on the keyboard, and you need to either discard the newline characters or include them as valid input.
To include it as valid input
Change correctInput = 'ABCabc '
to
correctInput = 'ABCabc n'
.
Or to discard the newline characters change
if all(char in correctInput for char in line1):
to
if all(char in correctInput for char in line1.replace('n', '')):
Either method will work.
Bytheway, input
is a function in Python. Although you're allowed to use it as a variable name, doing so will prevent you from using the input function in your program. Because of this, it is considered bad practice to use any of the built in function names as your variable names.
RegEx Solution
Just for fun, I came up with the following solution which solves your problem using regular expressions.
import re
with open(sys.argv[1]) as fh:
valid_lines = re.findall('^[ABCabc ]+n', fh.read())
This finds any valid lines using the pattern '^[ABCabc ]+n'
. What does this regular expression pattern do?
- The
^
symbol signifies the start of a line - Then comes the
[ABCabc ]
. When brackets are used, only characters inside of those brackets will be allowed. - The
+
after the brackets means that those characters that where in brackets must be found 1 or more times. - And lastly we make sure the valid characters we found are followed by a newline character (
n
). This ensures we checked the complete line for valid characters.
1
Awesome, this fixed it, thank you! Also good to know about input being a function. I am new to python (obviously) so that is good to know. Thanks again.
– Tsang
Nov 14 '18 at 6:11
add a comment |
If you print out the value of line1
before your if else statement, you'll see it has a newline character in it. (The n
character.) This is the character that gets added to the end of each line whenever you hit the enter key on the keyboard, and you need to either discard the newline characters or include them as valid input.
To include it as valid input
Change correctInput = 'ABCabc '
to
correctInput = 'ABCabc n'
.
Or to discard the newline characters change
if all(char in correctInput for char in line1):
to
if all(char in correctInput for char in line1.replace('n', '')):
Either method will work.
Bytheway, input
is a function in Python. Although you're allowed to use it as a variable name, doing so will prevent you from using the input function in your program. Because of this, it is considered bad practice to use any of the built in function names as your variable names.
RegEx Solution
Just for fun, I came up with the following solution which solves your problem using regular expressions.
import re
with open(sys.argv[1]) as fh:
valid_lines = re.findall('^[ABCabc ]+n', fh.read())
This finds any valid lines using the pattern '^[ABCabc ]+n'
. What does this regular expression pattern do?
- The
^
symbol signifies the start of a line - Then comes the
[ABCabc ]
. When brackets are used, only characters inside of those brackets will be allowed. - The
+
after the brackets means that those characters that where in brackets must be found 1 or more times. - And lastly we make sure the valid characters we found are followed by a newline character (
n
). This ensures we checked the complete line for valid characters.
1
Awesome, this fixed it, thank you! Also good to know about input being a function. I am new to python (obviously) so that is good to know. Thanks again.
– Tsang
Nov 14 '18 at 6:11
add a comment |
If you print out the value of line1
before your if else statement, you'll see it has a newline character in it. (The n
character.) This is the character that gets added to the end of each line whenever you hit the enter key on the keyboard, and you need to either discard the newline characters or include them as valid input.
To include it as valid input
Change correctInput = 'ABCabc '
to
correctInput = 'ABCabc n'
.
Or to discard the newline characters change
if all(char in correctInput for char in line1):
to
if all(char in correctInput for char in line1.replace('n', '')):
Either method will work.
Bytheway, input
is a function in Python. Although you're allowed to use it as a variable name, doing so will prevent you from using the input function in your program. Because of this, it is considered bad practice to use any of the built in function names as your variable names.
RegEx Solution
Just for fun, I came up with the following solution which solves your problem using regular expressions.
import re
with open(sys.argv[1]) as fh:
valid_lines = re.findall('^[ABCabc ]+n', fh.read())
This finds any valid lines using the pattern '^[ABCabc ]+n'
. What does this regular expression pattern do?
- The
^
symbol signifies the start of a line - Then comes the
[ABCabc ]
. When brackets are used, only characters inside of those brackets will be allowed. - The
+
after the brackets means that those characters that where in brackets must be found 1 or more times. - And lastly we make sure the valid characters we found are followed by a newline character (
n
). This ensures we checked the complete line for valid characters.
If you print out the value of line1
before your if else statement, you'll see it has a newline character in it. (The n
character.) This is the character that gets added to the end of each line whenever you hit the enter key on the keyboard, and you need to either discard the newline characters or include them as valid input.
To include it as valid input
Change correctInput = 'ABCabc '
to
correctInput = 'ABCabc n'
.
Or to discard the newline characters change
if all(char in correctInput for char in line1):
to
if all(char in correctInput for char in line1.replace('n', '')):
Either method will work.
Bytheway, input
is a function in Python. Although you're allowed to use it as a variable name, doing so will prevent you from using the input function in your program. Because of this, it is considered bad practice to use any of the built in function names as your variable names.
RegEx Solution
Just for fun, I came up with the following solution which solves your problem using regular expressions.
import re
with open(sys.argv[1]) as fh:
valid_lines = re.findall('^[ABCabc ]+n', fh.read())
This finds any valid lines using the pattern '^[ABCabc ]+n'
. What does this regular expression pattern do?
- The
^
symbol signifies the start of a line - Then comes the
[ABCabc ]
. When brackets are used, only characters inside of those brackets will be allowed. - The
+
after the brackets means that those characters that where in brackets must be found 1 or more times. - And lastly we make sure the valid characters we found are followed by a newline character (
n
). This ensures we checked the complete line for valid characters.
edited Nov 13 '18 at 20:50
answered Nov 13 '18 at 20:04
Mr. MeMr. Me
3,13012235
3,13012235
1
Awesome, this fixed it, thank you! Also good to know about input being a function. I am new to python (obviously) so that is good to know. Thanks again.
– Tsang
Nov 14 '18 at 6:11
add a comment |
1
Awesome, this fixed it, thank you! Also good to know about input being a function. I am new to python (obviously) so that is good to know. Thanks again.
– Tsang
Nov 14 '18 at 6:11
1
1
Awesome, this fixed it, thank you! Also good to know about input being a function. I am new to python (obviously) so that is good to know. Thanks again.
– Tsang
Nov 14 '18 at 6:11
Awesome, this fixed it, thank you! Also good to know about input being a function. I am new to python (obviously) so that is good to know. Thanks again.
– Tsang
Nov 14 '18 at 6:11
add a comment |
Its because readline doesn't remove 'n' from end of the line. You could ignore that problem by splitting whole file content in lines and than validate them one by one.
import sys
file_name = sys.argv[1]
file = open(file_name ,"r")
correctInput = 'ABCabc '
lines = file.read().splitlines()
for line1 in lines:
if all(char in correctInput for char in line1):
print 'Valid'
else:
print 'Invalid'
That makes sense. Thank you
– Tsang
Nov 14 '18 at 6:10
add a comment |
Its because readline doesn't remove 'n' from end of the line. You could ignore that problem by splitting whole file content in lines and than validate them one by one.
import sys
file_name = sys.argv[1]
file = open(file_name ,"r")
correctInput = 'ABCabc '
lines = file.read().splitlines()
for line1 in lines:
if all(char in correctInput for char in line1):
print 'Valid'
else:
print 'Invalid'
That makes sense. Thank you
– Tsang
Nov 14 '18 at 6:10
add a comment |
Its because readline doesn't remove 'n' from end of the line. You could ignore that problem by splitting whole file content in lines and than validate them one by one.
import sys
file_name = sys.argv[1]
file = open(file_name ,"r")
correctInput = 'ABCabc '
lines = file.read().splitlines()
for line1 in lines:
if all(char in correctInput for char in line1):
print 'Valid'
else:
print 'Invalid'
Its because readline doesn't remove 'n' from end of the line. You could ignore that problem by splitting whole file content in lines and than validate them one by one.
import sys
file_name = sys.argv[1]
file = open(file_name ,"r")
correctInput = 'ABCabc '
lines = file.read().splitlines()
for line1 in lines:
if all(char in correctInput for char in line1):
print 'Valid'
else:
print 'Invalid'
answered Nov 13 '18 at 20:07
Filip MłynarskiFilip Młynarski
1,7591413
1,7591413
That makes sense. Thank you
– Tsang
Nov 14 '18 at 6:10
add a comment |
That makes sense. Thank you
– Tsang
Nov 14 '18 at 6:10
That makes sense. Thank you
– Tsang
Nov 14 '18 at 6:10
That makes sense. Thank you
– Tsang
Nov 14 '18 at 6:10
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53288601%2fchecking-characters-in-an-input-file-line-and-returning-if-they-contain-only-va%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
3
please don't create a variable called
input
– Ayxan
Nov 13 '18 at 20:02