我无法理解的 `if` 语句中的无效语法错误?
Invalid syntax error in an `if` statement which I am unable to understand?
我逐行查看并遇到了这个错误。
SyntaxError: invalid syntax
其中,emailRegex
被突出显示
if cell.value and isinstance(cell.value, str) emailRegex.match(cell.value):
完整代码(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\._%+\-]+@[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)
谁能指出一个资源,我可以从中了解到底出了什么问题?在 SO 的帮助和阅读文档的帮助下,我已经尝试整理这个功能近 12 个小时了。
当前错误后代码是否正常?
谢谢
您错过了谓词之间的 and
:
if cell.value and isinstance(cell.value, str) and emailRegex.match(cell.value):
^^^
我逐行查看并遇到了这个错误。
SyntaxError: invalid syntax
其中,emailRegex
被突出显示
if cell.value and isinstance(cell.value, str) emailRegex.match(cell.value):
完整代码(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\._%+\-]+@[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)
谁能指出一个资源,我可以从中了解到底出了什么问题?在 SO 的帮助和阅读文档的帮助下,我已经尝试整理这个功能近 12 个小时了。
当前错误后代码是否正常?
谢谢
您错过了谓词之间的 and
:
if cell.value and isinstance(cell.value, str) and emailRegex.match(cell.value):
^^^