집에서 직접 사업을 운영하고 2 일 전에 Python을 사용하기 시작했습니다. 내 로그 파일을 한 줄씩 검색하고 시스템이 필자의 필수 명명 체계와 일치하지 않는지 알려주는 스크립트를 작성하려고합니다. 다양한 스키마가 있으며 스크립트에서 모두 찾아야합니다. 나는 목록을 사용하여 시도한 (아래 참조)하지만 작동하지 않습니다 및 다음 일반 괄호로 시도하고 내게 오류 (왼쪽 피연산자, 튜플 필요) 준. 나는 나에게 문제가되는 줄을 지적했다.Python 2.7.2 하나의 변수에 대한 여러 값
#variables
tag = ["DATA-", "MARK", "MOM", "WORK-"] #THIS ONE!!!!!!
#User Input
print "Please select Day of the week"
print "1. Monday"
print "2. Tuesday"
print "3. Wednesday"
print "4. Thursday"
print "5. Friday"
print "6. Saturday"
print "7. Sunday"
day = input("> ")
#open appropriate file and check to see if 'tag' is present in each line
#then, if it doesn't, print the line out.
if day == 1:
f = open('F:\DhcpSrvLog-Mon.log', 'r')
for line in f:
if tag in line: #THIS ONE!!!!!!!!!!!!!
pass
else:
print line
팁이나 트릭이 가장 만족 스럽습니다. 당신이 걱정할 필요가 없습니다, 그래서 당신은 자동적으로 블록의 출구에있는 파일을 닫습니다 with
를 사용
with open('F:\DhcpSrvLog-Mon.log', 'rU') as f:
for line in f:
for t in tag:
if t in line: break
else:
print line
:
if day == 1:
f = open('F:\DhcpSrvLog-Mon.log', 'r')
for line in f:
if [x for x in tag if x in line]: #THIS ONE!!!!!!!!!!!!!
pass
else:
print line
그랬어! 정말 고마워! – user1197368