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?

Dani AI

Generated

A compact, robust way to turn lines like the ones in your file into HTML is to parse each line, extract the numeric index (if present) and the URL inside [url]...[/url], ensure a URL scheme (so links work in browsers), escape text for HTML, and write the result to a new file. Notes: use target="_blank" (with the underscore) not target="blank", back up the original file, and prefer writing to a new file instead of editing in place.

import re
from pathlib import Path
from html import escape

input_path = Path(r"C:\Users\Ihsan\Desktop\links.txt")
output_path = input_path.with_suffix('.html')

full_pat = re.compile(r'^\s*(\d+)\.\s*\[url\]\s*(.*?)\s*\[/url\]\s*$', re.I)
any_pat = re.compile(r'\[url\]\s*(.*?)\s*\[/url\]', re.I)
num_pat = re.compile(r'^\s*(\d+)\.\s*(.*)$')

with input_path.open('r', encoding='utf-8') as src, output_path.open('w', encoding='utf-8') as dst:
    for idx, raw in enumerate(src, 1):
        line = raw.strip()
        if not line:
            continue

        m = full_pat.match(line)
        if m:
            number, url = int(m.group(1)), m.group(2).strip()
        else:
            m_any = any_pat.search(line)
            if m_any:
                number, url = idx, m_any.group(1).strip()
            else:
                m_num = num_pat.match(line)
                number = int(m_num.group(1)) if m_num else idx
                url = (m_num.group(2).strip() if m_num else line)

        if not re.match(r'^[a-zA-Z][a-zA-Z0-9+.-]*://', url):
            href = 'http://' + url
        else:
            href = url

        dst.write(f'<p>{number}: <a href="{escape(href, quote=True)}" target="_blank">{escape(url)}</a></p>\n')

This script:

  • handles lines that already include a leading number or not,
  • finds [url]...[/url] anywhere in the line as a fallback,
  • prepends http:// when no scheme is present,
  • escapes text with html.escape to avoid broken HTML.

As pointed out, a small formatting routine is enough for single strings; this script applies that idea across every line and writes out an HTML file. As asked about data_list: using for idx, line in enumerate(file, 1) avoids loading the whole file into memory, but if you do use data_list = f.readlines() you can modify in-place with for i, line in enumerate(data_list): data_list[i] = transform(line) and then write data_list back out.

Troubleshooting: test on a copy, inspect a few lines of the output in a browser or validator, and tweak the regex if your real file has different variations (extra text around the tag, different numbering formats, etc.).

Recommended Answers

All 2 Replies

Looks like homework.

What are the contents of data_list?

What do you need to do to each one?

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>'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.