2016-12-20 72 views
-5

當剪貼板中的文本沒有電子郵件地址或電話號碼時,代碼工作正常,即當預期結果爲「Nothing Found」時 對於其他情況,它不起作用。它顯示錯誤 - AttributeError的:「STR」對象沒有屬性「匹配」爲什麼這段代碼不工作? Python

#! python3 
# contactDetails.py - Finds email and phone number from a page 

import pyperclip, re 

phoneRegex = re.compile(r'(\+\d{2}-\d{10})')  # Phone Number Regex 
# email Regex 
emailRegex = re.compile(r'''(
[a-zA-Z0-9._]+ # username 
@    # @ symbol 
[a-zA-Z0-9._]+ # domain name 
(\.[a-zA-Z]{2,4}])# dot-something 
)''', re.VERBOSE) 

text = str(pyperclip.paste()) 
matches = [] 
for groups in phoneRegex.findall(text): 
    phoneNum=phoneRegex.findall(text) 
    matches.append(phoneNum) 
for groups in emailRegex.findall(text): 
    matches.append(groups[0]) 

if len(matches) >0: 
    pyperclip.copy('\n'.matches) 
    print('Copied to Clipboard:') 
    print('\n'.join(matches)) 
else: 
    print('Nothing Found') 
+1

請參見''\ n'.matches' - 你肯定要加入列表中的項目。 –

+0

更改標題和[創建最小,完整和可驗證的示例。](http://stackoverflow.com/help/mcve)這種標題容易被標記和關閉。 – MYGz

回答

1

如由Wiktor的Stribiżew註釋所提到的,問題是在這條線

pyperclip.copy('\n'.matches) 

特別,它是在這裏

'\n'.matches 

的第一項'\n'是一個字符串對象,沒有財產稱爲匹配,可以調用。你想要的是做一個.join,因爲你已經做了兩行後,即

pyperclip.copy('\n'.join(matches))