1,105,399 Community Members

Help! Modify all items in list

Member Avatar
Natsu
Newbie Poster
1 post since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I have a file with bunch of lines and I want add html tag for in every line in file
Example:

1. [url]www.google.com[/url]
2. [url]www.facebook.com[/url]
....
107. [url]www.daniweb.com[/url]

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?

Member Avatar
Trentacle
Junior Poster
107 posts since Dec 2010
Reputation Points: 112 [?]
Q&As Helped to Solve: 26 [?]
Skill Endorsements: 0 [?]
 
0
 

Looks like homework.

What are the contents of data_list?

What do you need to do to each one?

Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
1
 

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>'
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: