I'm in the process of converting BASH scripts I've written into python (my BASH chops put me in a minority where I'm working).
I've got a BASH while read
function loop that opens a file and formats the tab delimited content into an HTML table:
function table_item_row {
OLD_IFS="${IFS}"
IFS=$'\t'
while read CODE PRICE DESCRIPTION LINK PICTURE LINE; do
printf " <tr>\n"
printf " <td><img src=\"%s\"></td>\n" "${PICTURE}"
printf " <td><a href=\"%s\">%s</a> ($%.2f)</td>\n" "${LINK}" "${DESCRIPTION}" "${PRICE}"
printf " </tr>\n"
done < inventory.txt
IFS="${OLD_IFS}"
}
I can do something like this in python, but, having heard of the csv
module, I'm wondering if there's a preferred way:
for line in open(filename):
category, code, price, description, link, picture, plans = line.split("\t")
print " <tr>"
print " <td><img src=\"" + picture + "\"></td>"
print " <td><a href=\""+ link + "\">" + description + "</a> ($%.2f)</td>" % float(price)
print " </tr>"