GH avatar Bite 164. CLI tool: HTML link converter (stdin to stdout)

In this Bite you are going to help your team out automating a boring task.

They reached out to you with some data files that contain lines of (link_href, link_name). Unfortunately they also contain bad data.

The additional requirement is that your script can receive piped output from another process (read on ...)

Code up make_html_links that reads in data from stdin (use sys) and converts it to proper HTML:

  • Ignore the bad lines (no http in line, more than one comma, etc),
  • Strip white spaces around link_href and link_name,
  • Final twist: if the domain is NOT or make it an external link by adding target="_blank" to the resulting HTML.
  • Make the function print to standard output, don't return anything. The tests run your code as below and hence check standard output!

Running your program, which is automatically saved in a module called, it should work like this:

$ cat data1, Python Homepage
bad data,blabla,123 , Generators are Awesome
more bad data

$ cat data1|python
<a href="" target="_blank">Python Homepage</a>
<a href="">Generators are Awesome</a>

$ cat data2
bogus data, again , Bites of Py,How to capture stdout,Our labor of love, About Us, Dutch news site

$ cat data2|python
<a href="">Bites of Py</a>
<a href="" target="_blank">How to capture stdout</a>
<a href="">Our labor of love</a>
<a href="">About Us</a>
<a href="" target="_blank">Dutch news site</a>

Pretty cool/useful, no? Make your team happy and increase your coding ninja reputation in your org ;) - have fun and keep calm and code in Python!

Login and get coding
go back Advanced level
Bitecoin 4X

Will you be Pythonista #36 to crack this Bite?
Resolution time: ~47 min. (avg. submissions of 5-240 min.)
Pythonistas rate this Bite 3.67 on a 1-10 difficulty scale.
» You can do it! 😌

We use Python 3.7