user3358884
user3358884

Reputation: 109

python Filtering the file contents

I am new to python and I am trying to do the following assignment but my output is not the same as the one it is supposed to be. Can any one help me what the problem is here? I appreciate your helps!

Assignment:

In the third program, we take a look into the classification of file contents. In the same directory with the source code is a file "strings.txt", which has random strings in several lines. The lines can be divided into two groups: those which only have letters (a-z, A-Z) and numbers (0-9), and those which also have random special characters (?,&,@, $ ...).

Create a program which reads all of the lines from the file and tests the lines. If the line has only letters and/or numbers, the program prints "[line] was ok.". If the line has special characters, the program should print "[line] was invalid.". When the program works, it prints out something like this:

5345m345ö34l was ok.
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.
bbrbwb55be3"?"#? was invalid.
"?"#%#"!%#"&"?%%"?#?#"?" was invalid.
retrte#%#?% was invalid.
abcdefghijklmnopqrstuvxy was ok.

It is advisable to read the lines one at a time, test them with the isalmun() string test and go on from there. Also remember that the strings may also end in a line break (\n), which is allowed, but fails the .isalnum() test if not sliced away. Example 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.

my code is

handle = open("strings.txt","r")
content = handle.read()
content.isalnum()
for i in content:
    if content.isalnum()==True:
        print(content,"was ok")
    else:
        print(content,"was invalid")

handle.close()

My output is

5345m34534l
no2no123non4
noq234n5ioqw#%
%#""SGMSGSER
doghdp5234
sg,dermoepm
43453-frgsd
hsth()))
bmepm35wae
vmopaem2234+0+
gsdm12313
gswrgsrdgrsgsig45
)/(/)(#=%#)%/
++-+-+--+--+-+>-<+-<<_<-+>>++. was invalid
5345m34534l
no2no123non4
noq234n5ioqw#%
%#""SGMSGSER
doghdp5234
sg,dermoepm
43453-frgsd
hsth()))
bmepm35wae
vmopaem2234+0+
gsdm12313
gswrgsrdgrsgsig45
)/(/)(#=%#)%/
++-+-+--+--+-+>-<+-<<_<-+>>++. was invalid
5345m34534l
no2no123non4
noq234n5ioqw#%
%#""SGMSGSER
doghdp5234
sg,dermoepm
43453-frgsd
hsth()))
bmepm35wae
vmopaem2234+0+
gsdm12313
gswrgsrdgrsgsig45
)/(/)(#=%#)%/
++-+-+--+--+-+>-<+-<<_<-+>>++. was invalid

# etc ad nauseum...

What am I doing wrong?

Upvotes: 2

Views: 3598

Answers (5)

Hicham
Hicham

Reputation: 1

handle = open("strings.txt","r")

line = ""
while True:
    line = handle.readline()
    if line == "":
        break
    line = line[:-1]
    if line.isalnum() == False:
        print(line,"was invalid.")
    else:
        print(line,"was ok.")

handle.close()

Upvotes: 0

Thinh Le
Thinh Le

Reputation: 652

Here is my code for that homework and it gives the desired output:

filename="strings.txt"
handle = open("strings.txt","r") #read file
text = handle.readlines() # read lines by lines
for i in text:
    if i.rstrip().isalnum():
        print(i.rstrip()," was ok.")
    else:
        print(i.rstrip()," was invalid.")
handle.close()

Upvotes: 0

Suman Nepali
Suman Nepali

Reputation: 3

file = open("strings.txt", "r")
    content = file.readlines()
    file.close()
    for line in content:
        line = line.strip()
        if line.isalnum():
            print(line + " was ok.")
        else:
            print(line + " was invalid.")

Upvotes: -1

Le Minh Hoang
Le Minh Hoang

Reputation: 1

I think the output was wrong because you read entire file into one string and loop over all character of the file. Your "content" variable is a string, so the code don't check line by line. It checks the entire file and prints that entire file was invalid.

My answer:

file = open("strings.txt","r") content = file.readlines() for i in content: if i.rstrip().isalnum(): print(i+" was ok.") else: print(i+" was invalid.") file.close()

Upvotes: 0

Hugh Bothwell
Hugh Bothwell

Reputation: 56634

handle = open("strings.txt","r")
content = handle.read()  # <= here you read in the entire file
content.isalnum()
for i in content:      # <= here you iterate over each **character** of the file
    if content.isalnum()==True:
        print(content,"was ok")
               # ^ here you print the entire input file each time
    else:
        print(content,"was invalid")
               # ^ (ditto) which is why you have so much output
handle.close()

instead, try

with open("strings.txt") as inf:
    for line in inf:
        line = line.rstrip()
        if line.isalnum():
            print("{} was ok".format(line))
        else:
            print("{} was invalid".format(line))

Upvotes: 3

Related Questions