I'm reading text file in the following format(a.txt).
http://www.example.com/forum/showthread.php?t=779689/images/webcard.jpg 121.10.208.31
Then I need to obtain only the www.example.com part with /images/webcard.jpg 121.10.208.31 and write to the same file or a separate one. In this case I'm writing it to 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')
the above code gives the following error?How to achieve this?
Traceback (most recent call last):
File "prittyprint.py", line 17, in <module>
fo.write(urlparse(line).netloc+ ' ' + line.split(' ')[1] + ' ' + line.split(' ')[2] + '\n')
IndexError: list index out of range
a.txt? some line might not have this format. Can you print the line where it crashes?