Python 为什么这个代码不起作用?蟒蛇

Python 为什么这个代码不起作用?蟒蛇,python,regex,Python,Regex,当剪贴板中的文本没有电子邮件地址或电话号码时,即当预期结果为“未找到任何内容”时,代码工作正常 在其他情况下,它不起作用。它显示出错误- AttributeError:'str'对象没有属性'matches' #! python3 # contactDetails.py - Finds email and phone number from a page import pyperclip, re phoneRegex = re.compile(r'(\+\d{2}-\d{10})')

当剪贴板中的文本没有电子邮件地址或电话号码时,即当预期结果为“未找到任何内容”时,代码工作正常 在其他情况下,它不起作用。它显示出错误- AttributeError:'str'对象没有属性'matches'

#! 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')

正如Wiktor Stribiżew在评论中提到的,问题就在这方面

    pyperclip.copy('\n'.matches)
尤其是在这里

'\n'.matches
第一项
'\n'
是一个字符串对象,没有可调用的名为matches的属性。你要做的是做一个.连接,就像你在两行之后做的那样,即

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

请参见
“\n”。匹配项
-您肯定要加入列表中的项目。更改标题,此类标题很容易被标记和关闭。