Parsing CSV file name list into HTML list using Python

in #utopian-io6 years ago (edited)

What Will I Learn?

This tutorial will teach you

  • to read CSV files using CSV module.
  • parse CSV file name list into HTML list

Requirements

  • A PC/laptop with any Operating system such as Linux, Mac OSX, Windows OS
  • Preinstalled Python
  • Preinstalled Code Editors such as Atom, Sublime text or Pycharm IDE

Note: This tutorial is performed in Pycharm IDE in laptop with Ubuntu 17.1, 64 bit OS

Difficulty

Intermediate, anyone with a basic concept of CSV files and programming knowledge of python or any other programming language can catch up this tutorial. I recommend learning basics of python programming before starting this tutorial. The links for previous tutorials are at the bottom of this tutorial.

Tutorial Contents

We will start by importing CSV module of python.

import csv

Then, we will open existed csv file in reading mode. The csv file contains top six sponsors, delegated steem power and steem received data in table format separated by commas. The existed file to open is found here.

with open('sponsers.csv', 'r') as csvfile:

Now we will define variable csv_reader to hold returned object from csv.reader method and print the values of each lines in csv file using loop. Our final code looks like:

import csv
with open('sponsers.csv', 'r') as csvfile:
    csv_reader=csv.reader(csvfile)
    for line in csv_reader:
        print(line)

Output:

['Username', ' Delegated Steem Power', ' Steem Received']
['freedom', ' 1222997 SP', ' 10241.32']
['misterdelegation', ' 1019154 SP', ' no data']
['ned', ' 1010369 SP', ' no data']
['wackau', ' 97840 SP', ' 727.88']
['xeldal', ' 50791 SP', ' 431.22']
['lafona-miner', ' 24460 SP', ' 170.44']

The first line of the output is known as fieldnames if we map datas as dictionary.We don't want to print fieldnames in our output. So, we will add

next(csv_reader)

befor the loop to skip it while printing.

import csv
with open('sponsers.csv', 'r') as csvfile:
    csv_reader=csv.reader(csvfile)
    next(csv_reader)
    for line in csv_reader:
         print(line)

Output:

['freedom', ' 1222997 SP', ' 10241.32']
['misterdelegation', ' 1019154 SP', ' no data']
['ned', ' 1010369 SP', ' no data']
['wackau', ' 97840 SP', ' 727.88']
['xeldal', ' 50791 SP', ' 431.22']
['lafona-miner', ' 24460 SP', ' 170.44']

These are the top six sponsers of utopian.io. Now we will parse this csv file to html list.
For this, we need to define a variable after importing csv module which holds our HTML list.

html_list=''

Now, it is empty where data will be assigned later.
then, we will define empty list which holds usernames of sponsors, which is also assigned later.

sponsors=[]

To add a list of usernames in sponsors we assign the values of index [0] skipping the first row we edit above code to make look like below:

import csv
sponsers = []
with open('sponsers.csv', 'r') as csvfile:
    csv_reader=csv.reader(csvfile)
    next(csv_reader)
    for line in csv_reader:
        sponsers=line[0]
        print(sponsers)

Output:

freedom
misterdelegation
ned
wackau
xeldal
lafona-miner

Now, we will parse above usernames into HTML list. We will use DictReader method and f' string in this process. Dictreader is same like reader method of csv module. The only difference is Dictreader maps data into dictionary to perform operation. f' string is formatted string literals which also accepts replacement fields . The field to be replaced is surrounded by curly brackets.

import csv
html_list = ''
username = []
with open('sponsers.csv', 'r') as csvfile:
    csv_reader = csv.DictReader(csvfile)
    html_list += f'<p>HTML list of top six sponsers </p>'
    html_list += '\n<ul>'
    for line in csv_reader:
      username=line['Username']
      html_list += f'\n\t<li>{username}</li>'
    html_list += '\n</ul>'
print(html_list)

In above code, only username field is taken and added to html list. The usernames are looped and replaced each time in {username} and gets printed.

Output:

<p>HTML list of top six sponsers </p>
<ul>
    <li>freedom</li>
    <li>misterdelegation</li>
    <li>ned</li>
    <li>wackau</li>
    <li>xeldal</li>
    <li>lafona-miner</li>
</ul>

Ok now we learned to parse csv file field into html list , this script will automate the process and saves alot of time in future.
all above codes including previous tutorials codes are available in my Github repo. Click here to download

For more details please visit Python Docs.

Curriculum

Python tutorials for beginners : Part - I

Python tutorials for beginners : Part - II

Python tutorials for beginners : Part - III

Object-oriented Python

Reading and writing to files in python



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.

  • Please add more details in next time.

You can contact us on Discord.
[utopian-moderator]

Hey @forkonti, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

@forkonti Is there any edit necessary?

Sorry declined posts cannot be change by mods, i wish you the best in next posts.

Coin Marketplace

STEEM 0.17
TRX 0.13
JST 0.028
BTC 59605.49
ETH 2607.69
USDT 1.00
SBD 2.42