Python 过滤文件内容
我刚开始学习Python,正在做一个作业,但我的输出结果和预期的不一样。有没有人能帮我看看问题出在哪里?谢谢大家的帮助!
作业内容:
在这个程序中,我们要对文件内容进行分类。在和源代码同一个文件夹里,有一个叫“strings.txt”的文件,里面有几行随机字符串。这些行可以分成两类:一种是只有字母(a-z, A-Z)和数字(0-9)的,另一种则包含一些随机的特殊字符(比如?,&,@,$等)。
请创建一个程序,读取文件中的所有行,并对这些行进行测试。如果某一行只有字母和/或数字,程序就打印“[line] 是有效的。”如果这一行有特殊字符,程序就打印“[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.
建议一次读取一行,使用isalnum()这个字符串测试来检查,然后继续处理。还要记得,字符串可能以换行符(\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...
我哪里做错了?
5 个回答
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.")
当然可以!请把你想要翻译的内容发给我,我会帮你把它变得更简单易懂。
在编程中,有时候我们会遇到一些问题,特别是在使用某些工具或库的时候。这些问题可能会让我们感到困惑,尤其是当我们刚开始学习编程的时候。比如,有人可能会在使用某个特定的功能时,发现它没有按照预期工作。这种情况可能是因为我们没有正确理解这个功能的用法,或者是因为我们在设置上出现了错误。
在这种情况下,向其他人寻求帮助是一个不错的选择。我们可以在一些技术论坛上提问,比如StackOverflow。在那里,有很多经验丰富的程序员愿意分享他们的知识和经验,帮助我们解决问题。
当我们提问时,提供足够的信息是非常重要的。比如,我们可以描述我们遇到的问题,提供相关的代码片段,或者说明我们尝试过的解决方法。这样,其他人才能更好地理解我们的困境,并给出有效的建议。
总之,遇到问题时不要灰心,积极寻求帮助,并且尽量清楚地表达自己的问题,这样才能更快地找到解决方案。
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()
这是我为那个作业写的代码,它能产生我想要的结果:
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()
我觉得你的输出结果是错的,因为你把整个文件的内容都读进了一个字符串里,然后对文件里的每个字符进行循环。你的“content”变量是一个字符串,所以代码并没有逐行检查,而是检查整个文件,结果就打印出整个文件都是无效的。
我的回答是:
file = open("strings.txt","r")
content = file.readlines()
for i in content:
if i.rstrip().isalnum():
print(i+" 是有效的。")
else:
print(i+" 是无效的。")
file.close()
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))
相反,试试这个