0

1.この場合は「A1234ABCDE120083.1」である「\」の後の最後の単語であるビルドロケーションからbuildidを取得します

2. buildidを取得した後、ファイルを開き、「Engr Label:Data_CRM_PL_177999」という行と一致させて、「Data_CRM_PL_177999」というラベル名を取得しようとしています。

3.最終出力は「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
4

2 に答える 2

1

バックスラッシュは文字をエスケープしています(エスケープコード'のドキュメントを参照してください)

代わりに次の行を試してください。

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

これで、バックスラッシュをエスケープするバックスラッシュがあるので、文字列は文字通りのバックスラッシュになります。r他にできることは、次のように接頭辞を付けて、この文字列にエスケープコードがないことをPythonに通知することです。

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

他にも多くの問題があります。

Pythonのコメント文字は#、ではなく//です。

ファイル名とファイルオブジェクトも混同していると思います。

Notes開こうとしているファイルの名前です。次に、を呼び出すとopen(Notes)、データを読み取ることができるファイルオブジェクトが返されます。

したがって、おそらく次のものを置き換える必要があります。

open(Notes)

f = open(Notes)

そして、交換します:

for i in Notes.splitlines:

for line in f:

ファイルオブジェクトに対してforループを実行すると、Pythonは自動的に一度に1行ずつ表示します。

これで、次のように各行を確認できます。

if line.find("Engr Label") != -1:
  label = line.split(':')[-1]
于 2012-11-14T00:50:08.617 に答える
1

行で

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

バックスラッシュは実際には次の引用符をエスケープしています。したがって、Pythonはこれが実際にはあなたの文字列であると見なします。

'[-1])

代わりに、次のことを行う必要があります。

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

そしてPythonはあなたの文字列を次のように解釈します

\\

興味深いことに、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
于 2012-11-14T00:52:22.837 に答える