读取文件,提取url并重写Python

2024-04-19 17:38:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在读取下面的文本文件format(a.txt)。在

http://www.example.com/forum/showthread.php?t=779689/images/webcard.jpg121.10.208.31

然后我只需要获得www.example.com部分和{}并写入同一个文件或单独的文件。在本例中,我将其写入b.txt。在

from urlparse import urlparse 
f = open('a.txt','r')
fo = open('b','w')


for line in f:
    fo.write(urlparse(line).netloc+ ' ' + line.split(' ')[1] + ' ' + line.split(' ')[2] + '\n')

上面的代码给出了以下错误?如何做到这一点?在

^{pr2}$

Tags: 文件txtcomformathttpexamplewwwline
1条回答
网友
1楼 · 发布于 2024-04-19 17:38:16

可能是您的文件a.txt中存在异常。某些行可能没有此格式。你可以试试这个-

from urlparse import urlparse 

f = open('a.txt','r')
fo = open('b','w')

for line in f:
    split_line = line.split(' ')
    if len(split_line) >=3:
        fo.write(urlparse(line).netloc+ ' ' + split_line[1] + ' ' + split_line[2] + '\n')
    else:
        print "ERROR: some other line: %s" % (line) #continue on with next line

相关问题 更多 >