2014-03-06 3 views
0

저는 파이썬에 익숙하지 않고 다음과 같은 할당을 시도하고 있지만 출력물은 내 의도와 동일하지 않습니다. 어느 누구라도 문제가 여기에있는 것을 도울 수 있습니까? 당신의 도움에 감사드립니다!python 파일 내용 필터링하기

할당 : 세 번째 프로그램에서

, 우리는 파일 내용의 분류에 봐. 소스 코드와 동일한 디렉토리에 "strings.txt"파일이 있습니다.이 파일에는 여러 줄에 임의의 문자열이 있습니다. 이 줄은 문자 (a-z, A-Z)와 숫자 (0-9) 만있는 그룹과 임의의 특수 문자 (?, &, @, $ ...)가있는 그룹으로 나눌 수 있습니다.

파일에서 모든 줄을 읽고 줄을 테스트하는 프로그램을 만듭니다. 행에 문자 및/또는 숫자 만 있으면 "[line] was ok."가 인쇄됩니다. 행에 특수 문자가 있으면 프로그램은 "[line] was invalid."를 인쇄해야합니다.

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

난 당신이 하나의 문자열 및 파일의 모든 문자를 통해 루프에 전체 파일을 읽을 수 있기 때문에 출력이 잘못 생각하려고합니다. "content"변수는 문자열이므로 코드가 한 줄씩 확인되지 않습니다. 전체 파일을 검사하고 전체 파일이 유효하지 않은 내용을 인쇄합니다.

내 대답 :

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