Python Tutorial for Beginners - 11. Real World Example - Parsing Names From a CSV to an HTML List

Table of Contents

  1. Test text
  2. Run Code 1 in Python

Test text

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
FirstName,LastName,Email,Pledge,Lifetime,Status,Country,Start
1 + Reward,"Description I will add your name to the contributors page on my website.

You will also be eligible for Patreon-only rewards. I will occasionally give away books that I have read, and will choose a Patron at random to receive those.",,,,,,
John,Doe,johndoe@bogusemail.com,10.00,20.00,Ok,,2017-05-06 21:28:06.183108
Dave,Smith,davesmith@bogusemail.com,5.00,10.00,Ok,,2017-05-29 16:13:58.318920
Mary,Jacobs,maryjacobs@bogusemail.com,5.00,10.00,Ok,,2017-05-14 07:37:01.074648
Jane,Stuart,janestuart@bogusemail.com,5.00,25.00,Ok,,2016-12-30 18:12:13
Tom,Wright,tomwright@bogusemail.com,5.00,15.00,Ok,,2017-04-14 04:02:06.658439
Steve,Robinson,steverobinson@bogusemail.com,5.00,20.00,Ok,,2017-03-17 02:20:14
Nicole,Jacobs,nicolejacobs@bogusemail.com,5.00,20.00,Ok,,2017-03-12 17:07:29
Jane,Wright,janewright@bogusemail.com,5.00,25.00,,,2017-01-14 17:40:06
Jane,Doe,janedoe@bogusemail.com,2.50,5.00,Ok,,2017-05-14 11:20:50.798440
Kurt,Wright,kurtwright@bogusemail.com,2.00,2.00,Ok,,2017-06-23 21:12:15.662157
Kurt,Robinson,kurtrobinson@bogusemail.com,2.00,4.00,Ok,,2017-05-03 03:18:54.877885
Jane,Jenkins,janejenkins@bogusemail.com,2.00,8.00,Ok,,2017-03-23 16:37:47.708634
Neil,Robinson,neilrobinson@bogusemail.com,1.50,3.00,Ok,,2017-05-26 19:59:51.013984
Tom,Patterson,tompatterson@bogusemail.com,1.34,4.02,Ok,,2017-04-27 01:55:12.313799
Sam,Jenkins,samjenkins@bogusemail.com,1.00,0.00,Ok,,2017-07-16 02:34:47.674287
Steve,Stuart,stevestuart@bogusemail.com,1.00,1.00,Ok,,2017-06-12 03:32:00.706554
Maggie,Patterson,maggiepatterson@bogusemail.com,1.00,2.00,Ok,,2017-05-26 01:23:14.097393
Maggie,Stuart,maggiestuart@bogusemail.com,1.00,2.00,Ok,,2017-05-19 23:11:08.624354
Jane,Doe,janedoe@bogusemail.com,1.00,3.00,Ok,,2017-04-15 19:40:17.044921
Steve,Patterson,stevepatterson@bogusemail.com,1.00,3.00,Ok,,2017-04-09 17:14:23.591656
Dave,Smith,davesmith@bogusemail.com,1.00,3.00,Ok,,2017-04-01 18:35:10.731005
Sam,Wilks,samwilks@bogusemail.com,1.00,4.00,Ok,,2017-03-01 09:40:56
Kurt,Jefferson,kurtjefferson@bogusemail.com,1.00,5.00,Ok,,2017-02-24 08:23:05
Sam,Stuart,samstuart@bogusemail.com,1.00,5.00,Ok,,2017-02-12 00:14:31
Jane,Stuart,janestuart@bogusemail.com,1.00,5.00,Ok,,2017-02-06 14:52:28
Dave,Davis,davedavis@bogusemail.com,1.00,0.00,,,2016-11-18 01:37:25
Sam,Patterson,sampatterson@bogusemail.com,1.00,8.00,Ok,,2016-11-01 11:27:17
Tom,Jefferson,tomjefferson@bogusemail.com,1.00,10.00,Ok,,2016-09-27 09:56:48
Jane,Stuart,janestuart@bogusemail.com,1.00,7.00,,,2016-08-09 14:42:25
Maggie,Jefferson,maggiejefferson@bogusemail.com,1.00,12.00,Ok,,2016-07-26 05:02:16
No Reward,Description: (None for No Reward),,,,,,
Mary,Wilks,marywilks@bogusemail.com,20.00,60.00,Ok,,2017-04-21 02:44:43.395224
Neil,Patterson,neilpatterson@bogusemail.com,10.00,80.00,Ok,,2016-11-12 17:49:37
Corey,Davis,coreydavis@bogusemail.com,6.00,90.00,Ok,,2016-04-01 15:19:52
Steve,Jacobs,stevejacobs@bogusemail.com,5.00,21.00,Ok,,2017-01-04 19:38:44
Jane,Jenkins,janejenkins@bogusemail.com,5.00,30.00,Ok,,2017-01-15 17:24:39
John,Jacobs,johnjacobs@bogusemail.com,3.14,34.54,Ok,,2016-08-23 16:09:25
Neil,Smith,neilsmith@bogusemail.com,3.00,24.00,Ok,,2016-11-28 02:57:48
Corey,Wilks,coreywilks@bogusemail.com,2.00,8.00,Ok,,2017-03-26 20:13:08.207869
Corey,Smith,coreysmith@bogusemail.com,1.00,0.00,Ok,,2017-07-05 01:50:35.171076
Mary,Patterson,marypatterson@bogusemail.com,1.00,0.00,Ok,,2017-07-04 18:05:23.052059
Jane,Stuart,janestuart@bogusemail.com,1.00,2.00,Ok,,2017-05-21 19:42:36.098523
Travis,Arnold,travisarnold@bogusemail.com,1.00,3.00,Ok,,2017-04-19 08:04:33.428559
John,Robinson,johnrobinson@bogusemail.com,1.00,4.00,Ok,,2017-03-30 14:59:33.850333
Travis,Arnold,travisarnold@bogusemail.com,1.00,6.00,Ok,,2017-01-28 22:02:57

patrons.csv

Run Code 1 in Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import csv

html_output = ''
names = []

with open('patrons.csv', 'r') as data_file:
    csv_data = csv.DictReader(data_file)

    # We don't want first line of bad data
    next(csv_data)

    for line in csv_data:
        if line['FirstName'] == 'No Reward':
            break
        names.append(f"{line['FirstName']} {line['LastName']}")

html_output += f'<p>There are currently {len(names)} public contributors. Thank You!</p>'

html_output += '\n<ul>'

for name in names:
    html_output += f'\n\t<li>{name}</li>'

html_output += '\n</ul>'

print(html_output)

start.py