我是python的新手,我正在尝试执行以下任务,但我的输出与它应该是的输出不同。任何人都可以帮我解决问题所在吗?感谢您的帮助!
分配:
在第三个程序中,我们来看一下文件内容的分类。在与源代码相同的目录中是一个文件“strings.txt”,它有几行中的随机字符串。这些行可以分为两组:只有字母(az,AZ)和数字(0-9)的那些,以及也有随机特殊字符(?,&,@,$ ...)的那些。 / p>
创建一个程序,读取文件中的所有行并测试行。如果该行只有字母和/或数字,程序将打印“[line] ok。”。如果该行有特殊字符,程序应打印“[line]无效。”。当程序工作时,它会输出如下内容:
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.
建议一次读取一行,使用isalmun()字符串测试对其进行测试,然后从那里继续。还要记住,字符串也可以以换行符(\ n)结束,这是允许的,但如果没有切掉,则无法通过.isalnum()测试。 示例输出
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.
我的代码是
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()
我的输出是
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...
我做错了什么?
答案 0 :(得分:2)
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()
相反,请尝试
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))
答案 1 :(得分:0)
我认为输出错误是因为你把整个文件读成一个字符串而且 循环遍历文件的所有字符。您的“内容”变量是一个字符串,因此代码不会逐行检查。它会检查整个文件并打印整个文件无效。
我的回答:
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()
答案 2 :(得分:0)
这是我的作业代码,它给出了所需的输出:
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()
答案 3 :(得分:0)
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()
答案 4 :(得分:-1)
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.")