2012-11-14 5 views
0

1.Getting 마지막 단어가 "A1234ABCDE120083.1는"이 경우는 파일 열기 및

2.After buildid을 받고있는 "\"뒤에있는 buildlocation에서 buildid ENGR 라벨 일치하는을 개방하고 파일, 행 "ENGR 레이블 : Data_CRM_PL_177999"와 일치하는 것을 시도 "Data_CRM_PL_177999"입니다 레이블 이름을 가져

3.Final 출력은 "Data_CRM_PL_177999"

어떤 이유로 나는 다음과 같은 구문을 얻고해야 오류 ..

import re 

Buildlocation= '\\umor\locations455\INT\A1234ABCDE120083.1' 

Labelgetbuildlabel(Buildlocation) 

def getbuildlabel(BuildLocation): 
buildid=BuildLocation.split('\')[-1] 
Notes=os.path.join(BuildLocation,Buildid + '_notes.txt') 
if os.path.exists(Notes): 
    try: 
     open(Notes) 
    except IOError as er: 
     pass 
    else: 
     for i in Notes.splitlines: 
     if i.find(Engr Label) 
      label=i.split(:)[-1] 

print label//output should be Data_CRM_PL_177999 
,

출력해야한다 : -

Line looks like below in the file 
Engr Label: Data_CRM_PL_177999 

구문 오류

buildid=BuildLocation.split('\')[-1] 
           ^
SyntaxError: EOL while scanning string literal 

답변

1

를 :

'[-1]) 

대신 다음을 수행해야합니다. :

buildid=BuildLocation.split('\\')[-1] 

그리고 파이썬이 문자열이 흥미롭게

\\ 

,이 문제에 StackOverflow의의 구문 형광펜 힌트로 해석됩니다. 코드를 살펴보면 코드 샘플의 끝까지 문자열의 일부로 처음 슬래시를 모두 처리합니다.

코드에 몇 가지 다른 문제가 있으므로 조금만 청소 해 보았습니다. (그러나 파일의 사본이 없으므로이 파일을 테스트 할 수 없었습니다.)

import re 
import os.path 

build_location= r'\\umor\locations455\INT\A1234ABCDE120083.1' 


label = get_build_label(build_location) 

# Python prefers function and variable names to be all lowercase with 
# underscore separating words. 
def get_build_label(build_location): 
    build_id = build_location.split('\\')[-1] 
    notes_path = os.path.join(build_location, build_id + '_notes.txt') 
    # notes_path is the filename (a string) 
    try: 
     with open(notes_path) as notes: 
      # The 'with' keyword will automatically open and close 
      # the file for you 
      for line in notes: 
       if line.find('Engr Label'): 
        label = line.split(':')[-1] 
        return label 
    except IOError: 
     # No need to do 'os.path.exists' since notes_path doesn't 
     # exist, then the IOError exception will be raised. 
     pass 
print label 
+0

파일 객체에'splitlines - 나는'xreadlines()'또는 보통 파일 객체만을 사용할 수 있다고 생각한다. –

+0

아, 그래. OP는 원래'splitlines'을 사용했는데, 나는 그것을 잡은 것 같지 않습니다. 나는 그것을 편집 할 것이다. – Michael0x2a

+0

@Michael - 조건이 예상대로 작동하지 않는 것 같아요. 줄마다 한 줄씩 찾으려고하지 않습니다. notes.txt 파일의 첫 줄에 "Request Date : 11/12/2012 5:22:51 PM" , 출력으로 51PM을 참조하십시오. 줄 입력란에 줄 번호 : if line.find ('Engr Label') : – user1795998

1

백 슬래시가 ' 문자

대신이 줄을 시도합니다 (escape codes documentation 참조) 탈출한다 :

buildid=BuildLocation.split('\\')[-1]  

이제 역 슬래시를 이스케이프 처리하므로 문자열이 리터럴 백 슬래시가됩니다. 당신이 할 수있는 다른 것은이 같은 r를 앞에하여이 문자열은 이스케이프 코드를 가지고 있지 않습니다 파이썬을 말할 것이다 :

당신은뿐만 아니라 다른 문제의 번호를 가지고
buildid=BuildLocation.split(r'\')[-1] 

.

파이썬의 주석 문자는 이 아니고 //이 아닙니다.

파일 개체와 파일 이름을 혼동하는 것 같습니다.

Notes은 열려고하는 파일의 이름입니다. 그런 다음 open(Notes)으로 전화하면 데이터를 읽을 수있는 파일 개체가 다시 전송됩니다.

그래서 당신은 아마 교체해야합니다 :

open(Notes) 

f = open(Notes) 

으로 그리고 대체 :

for i in Notes.splitlines: 

for line in f: 

파일 객체에 대해 for 루프를 수행하면 Python이 한 번에 한 줄씩 자동으로 줄을 지정합니다.이제

이 같은 각 라인 확인할 수 있습니다 라인

buildid=BuildLocation.split('\')[-1] 

백 슬래시 실제로 그래서, 파이썬이 실제로 문자열입니다 생각하는 다음과 같은 인용 부호를 이스케이프됩니다에서

if line.find("Engr Label") != -1: 
    label = line.split(':')[-1]