2017-01-22 3 views
-2

私は行ごとにこのエラーが発生しました。理解できない `if`文に構文エラーがありますか?

SyntaxError: invalid syntax

で、emailRegexは完全なコード(python3.x)

import re, openpyxl, os, sys 
def sort_email_from_xl(): 
    loc = input("Please enter path of the file:") 
    os.chdir(loc) 
    file = input("Filename:") 
    wb = openpyxl.load_workbook(file, use_iterators=True) 
    sheet = input("Which Sheet do you want to email?\n") 
    return sheet 
    wb.get_sheet_by_name(sheet) 
    ws = wb.get_sheet_by_name(sheet) 
    emailRegex = re.compile(r".*?([a-zA-Z0-9\._%+\-][email protected][a-zA-Z0-9.\-]+\.[a-zA-Z]{2,4}).*?") 
    customeremails = []  #works fine till here in idle 
for row in ws.iter_rows(): 
    for cell in row: 
     if cell.value and isinstance(cell.value, str) emailRegex.match(cell.value): 
      mail = emailRegex.match(cell.value) 
      if mail: 
       mail = mail.group(0) # use parentheses to call the function 
       cell.text = mail 
       customeremails.append(mail) 

print(customeremails) 

if cell.value and isinstance(cell.value, str) emailRegex.match(cell.value):

を強調します誰かが私は正確に何であるか読み取ることができるリソースを指すことができます間違っている?私はSOからの助けを借りて、そしてdocsを通して読んで、この機能をほぼ12時間にわたって整理しようとしています。

コードは現在のエラーの後に問題ありませんか?

おかげ

+0

あなたのコードを校正してください。 – TigerhawkT3

+0

[セル内のテキストを正規表現にマッチさせ、正規表現にマッチするテキストのみを保持するには?](http://stackoverflow.com/questions/41779655/how-to-match-text-in-a-cell) –

答えて

0

あなたは述語の間andを逃した:

if cell.value and isinstance(cell.value, str) and emailRegex.match(cell.value): 
               ^^^ 
+0

それを変更しようとしました:ws.iter_rows()の行: ' 'の場合: \t 'のセルの場合は、if:regex-and-keep-only-the-match-regex cell.valueとでisinstance(cell.value、STR)及びemailRegex.match(cell.value): '' \t \t \tメール= emailRegex.match(cell.value) '' \t \t \tならメール: '' \t \t \t \t mail = mail.group(0) ' '\t \t \t \t cell.text = mail' ' \t \t \t \t customeremails.append(メール) 'プリント(customeremails)' まだ取得し 'にSyntaxError:無効なしのハイライトはsyntax今けれども。 – Sid

+0

@Sid答えを指摘した部分を除いて、構文エラーはありません。私は自分のマシンで試しました。質問に記載されていない別のコードはありますか? – falsetru

+0

いいえ、私はすべてのコードを確実に含めるようにしました。私はそれを呼び出すスクリプトを使用して別の試してみましょう。 – Sid

関連する問題