2017-01-22 4 views
0

何らかの理由で、Excelセル内のテキストが、cell.valueとしてのみ読み込み可能です。Pythonを使ってregexを `cell.value`にマッチさせますか?

emailRegex = re.compile(".*?([a-zA-Z0-9\._%+\-][email protected][a-zA-Z0-9.\-]+\.[a-zA-Z]{2,4}).*?")

私は試してみてください。

for row in ws.iter_rows(): 
     for cell in row: 
      if emailRegex.match(cell.text): 
        mail = emailRegex.match(cell.text).group[0] 
        cell.text = mail 
        customeremails.append(mail) 

print(customeremails) 

AttributeError: Object has no str

for row in ws.iter_rows(): 
     for cell in row: 
      if emailRegex.match(str(cell.value)): 
        mail = emailRegex.match(str(cell.value)).group[0] 
        cell.text = mail 
        customeremails.append(mail) 

print(customeremails) 

SyntaxError: invalid syntax

は現在、8時間の解決策を見つけることを試みて。

編集:

エラーとemailRegexが更新されました。また

for row in ws.iter_rows(): 
      for cell in row: 
       content = cell.text 
       print(content) 

は、セルが何列

for row in ws.iter_rows(): 
      for cell in row: 
       content = cell.value 
       print(content) 

が含まれていないというエラーを与えるには、以下を与える:(中略)

None

None

4020110

None

Nagdevi

None

Mumbai

[email protected]

+1

完全なエラーメッセージを表示してください。 – BrenBarn

+0

'emailRegex'とは何ですか? – 2ps

+0

'group'はマッチ結果の関数です。 – 2ps

答えて

0

あなたは間違ってgroupと呼ばれます。配列ではなく関数として呼び出す必要があります。

# for python 3 
emailRegex = re.compile(r".*?([a-zA-Z0-9\._%+\-][email protected][a-zA-Z0-9.\-]+\.[a-zA-Z]{2,4}).*?") 
for row in ws.iter_rows(): 
    for cell in row: 
     if cell.value and isinstance(cell.value, str) and 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) 
+0

こんにちは、私はOPの情報を更新しました。すべての助けに感謝します。私は上記のコードを試して、_ SyntaxErrorを取得しました。もしcell.valueとisinstance(cell.value、str)のsyntax_が無効です。emailRegex.match(cell.value): ' – Sid

+0

Typo: – 2ps

関連する問題