2014-03-06 103 views
0

我是新来的python,我想做下面的任务,但我的输出是不一样的,它应该是。任何人都可以帮我解决这个问题吗?我感谢你的帮助!python过滤文件内容

分配:

在第三个节目,我们一起来看看到的文件内容进行分类。在与源代码相同的目录中有一个文件“strings.txt”,它有几行随机字符串。这些行可以分为两组:只有字母(a-z,A-Z)和数字(0-9)的行,以及也有随机特殊字符(?,&,@,$ ...)的行。

创建一个从文件中读取所有行并测试行的程序。如果该行只有字母和/或数字,程序将打印出“[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... 

我在做什么错?

回答

1
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)) 
+0

嗨,它看起来这是工作,除了一个错误。在“无效”之前的行结尾处有一个额外的“。”。像这样:++ - + - + - + - + - +> - <+-<<_<-+>> ++。无效。但它应该是这样的:++ - + - + - + - + - +> - <+-<<_<-+>> ++无效。点(。)从哪里来? – user3358884

+0

@ user3358884:检查你的输入文件,我几乎保证你的时间在那里。 –

+0

嗨,输入文件不是我的,它是系统生成的输入。我正在做一个自学工具viope.com上的任务,您只需执行代码,系统会通过提供自己的输入文件来检查您的代码。 – user3358884

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()