作者schedule6666 (schedule)
看板Python
标题[问题] 读取档案後使用正规表示法将字串列出
时间Tue Aug 8 20:03:54 2017
小妹为Python超新手,如果问了奇怪的问题,还请大家包涵。
最近在练习在pycharm读取电脑中的档案。
档案内容如下:
Joe's email is
[email protected]
Tom's email is
[email protected]
档名:email.txt
pycharm里的档案为:
import sys
import os
import re
fp= open("C:\\Users\\haha\\Desktop\\email.txt","r")
text =fp.readlines()
print(text)
for w in text:
sent= re.match(r'([\w.-]+@[\w.-])+',w)
print(sent)
fp.close()
预期会print出来的样子如下:
[email protected]
[email protected]
但编译执行後出现的样子如下:
["Joe's email is
[email protected]\n", "Tom's email is
[email protected]"]
None
None
<_sre.SRE_Match object at 0x02174960>
想请问版上各位高手是哪个地方出错了呢?
--
那个…我刚刚发现原因了…
因为正规表示法接受的格式为string,但我从上面的档案读的格式是清单(list)
这就是我无法编译执行的原因…我把清单读成字串就可以了。
程式如下:
import string
import sys
import os
import re
fp= open("C:\\Users\\haha\\Desktop\\email.txt","r")
text =fp.readlines()
text1=''.join(text)
print(text1)
sent = re.findall(r"[\w.-]+@[\w_.-]+", text1)
print(sent)
fp.close()
感谢各位高手的帮忙^^
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 36.231.24.145
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1502193837.A.3C2.html
1F:推 APM99: 你要用 re.search 08/08 20:31
2F:→ APM99: re.match在一开始没成功就跳过了 08/08 20:32
3F:→ ntumath: sent= re.findall(r'[\w.-]+@[\w.-]+',w) 08/08 20:32
已按照楼上二位的建议将re.mach改成 re.search及re.findall但都遇到了同样的错误...
错误如下(以findall为例)
["Joe's email is
[email protected]\n", "Tom's email is
[email protected]\n", '
[email protected]']
Traceback (most recent call last):
File "C:/Users/mlchen/PycharmProjects/untitled/Regular_expression.py", line 21, in <module>
sent = re.findall(r"[\w.-]+@[\w_.-]+", text)
File "C:\Python27\lib\re.py", line 181, in findall
return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer
Process finished with exit code 1
※ 编辑: schedule6666 (36.231.24.145), 08/08/2017 21:08:24
※ 编辑: schedule6666 (36.231.24.145), 08/08/2017 21:27:16
※ 编辑: schedule6666 (36.231.24.145), 08/08/2017 21:28:11
※ 编辑: schedule6666 (36.231.24.145), 08/08/2017 21:30:50
4F:推 APM99: 我python36不用那样样也可以的缩QQ 08/08 22:16
5F:→ schedule6666: 对耶,我的是python2.7说…看来该update一下了 08/08 22:28
6F:→ schedule6666: 总之,还是非常感谢APM大大的帮忙 ^^ 08/08 22:30
※ 编辑: schedule6666 (36.231.24.145), 08/09/2017 03:08:19
7F:→ coeric: 你从txt读到的是一整串的字串,先把他变成list吧..... 08/09 10:19
8F:→ coeric: 你要直接变成字串,用re去找也ok 08/09 10:20
9F:→ coeric: text=text.split() #会变成list 08/09 10:22
10F:→ coeric: 如果你只是单纯要抓到email,直接转成字串 用findall找 08/09 10:27
11F:→ coeric: 如果还要针对每一个email做动作,先把它切开成list 08/09 10:28
12F:→ coeric: 才方便做後续动作....否则,你findall以後,要再做一次for 08/09 10:28
Hello,Coeric,不好意思,因为刚写Python所以有点不太懂…,
照你的说法,所以我在一开始读档案的时候,python是预设将txt档里面的东西读成字串,
然後我要自已读成list吗?
第二个问题是,因为我只是练习要用正规表示法去抓e-mail,所以不太懂为什麽要读
list呢? 才方便做後续动作是指要把资料存在MySQL吗?
※ 编辑: schedule6666 (36.231.24.145), 08/09/2017 16:07:16
※ 编辑: schedule6666 (36.231.24.145), 08/09/2017 16:09:13
13F:推 ntumath: fp.read() --> str | fp.readlines() --> list 08/09 16:50
14F:→ ntumath: 如果用read,你就不用多加text1了 08/09 16:51
15F:→ ntumath: 不过在这种case我会选做dict啦,名字对email,方便就好 08/09 16:53
16F:→ coeric: fp.readlines() 会多个很讨厌的\n,在做资料处理时 08/09 23:11
17F:→ coeric: 我很讨厌中间多一堆没必要的东西,例:\n \t 之类的 08/09 23:11
18F:→ coeric: 修正上面说的,我会选择使用text=text.split('\n') 08/09 23:12