954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help! Modify all items in list

I have a file with bunch of lines and I want add html tag for in every line in file
Example:
1. <a href="http://www.google.com">www.google.com</a>
2. <a href="http://www.facebook.com">www.facebook.com</a>
....
107. <a href="http://www.daniweb.com">www.daniweb.com</a>

and the result I want in the end: <p>1: <a href="www.google.com" target="blank">www.google.com</a></p>
..so I have 180 lines in the file and want all of lines have the tags as above

My python code for now:

#open file first
fobj = open("C:/Users/Ihsan/Desktop/links.txt", 'r+')
data_list = fobj.readlines()    #import into working environment
init, head, tail, end = "<p>", "<a href=\"", "\" target=\"blank\">", "</a></p>"

for i in data_list:   #call every item
    for link in i:
        begin = link.index("w") #know the first "h" index
        .....

so how to modify items in list?

Natsu
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

Looks like homework.

What are the contents of data_list?

What do you need to do to each one?

Trentacle
Junior Poster in Training
72 posts since Dec 2010
Reputation Points: 110
Solved Threads: 20
 

Do function like:

>>> def change(ind, info):
	return '<p>%(ind)i: <a href="%(info)s" target="blank">%(info)s</a></p>' % locals()

>>> change(1, 'www.google.com')
'<p>1: <a href="www.google.com" target="blank">www.google.com</a></p>'
pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: