Exercise: Read CSV File

Sample CSV File

The following table (download CSV) is an excerpt from a stock exchange procedure [1]. In its visual form, it would be nicely made up, headed by a row that contains the column names.

FUND_NAME

FUND_ISIN

FUND_TICKER

AS_OF_DATE

FUND_IN_UNIVERSE

FUND_OF_FUNDS

FUND_HOLDINGS_COUNT

FUND_HOLDING_FUNDS_COUNT

American Funds American Balanced Fund;A

US0240711020

ABALX

20210103

T

2,084

0

Dodge & Cox Balanced Fund

US2562011047

DODBX

20210103

T

T

410

1

Dodge & Cox Stock Fund

US2562191062

DODGX

20210103

T

T

74

1

Franklin Income Fund;A1

US3534963000

FKINX

20210103

T

T

302

1

American Funds Growth Fund of America;A

US3998741066

AGTHX

20210103

T

378

0

T Rowe Price Growth Stock Fund

US7414791092

PRGFX

20210103

T

T

87

1

Fidelity Puritan Fund

US3163451079

FPURX

20210103

T

1,124

0

Fidelity Contrafund

US3160711095

FCNTX

20210103

T

363

0

T Rowe Price Capital Appreciation Fund

US77954M1053

PRWCX

20210103

T

T

165

2

Old Westbury Large Cap Strategies Fund

US6804141090

OWLSX

20210103

T

T

199

2

American Funds Capital Income Builder;A

US1401931035

CAIBX

20210103

T

1,582

0

Harbor Capital Appreciation Fund;Institutional

US4115115044

HACAX

20210103

T

55

0

T Rowe Price Blue Chip Growth Fund

US77954Q1067

TRBCX

20210103

T

T

123

1

Dreyfus Treasury Securities Cash Management;Inst

US2619411083

DIRXX

20210103

T

50

0

BlackRock Global Allocation Fund;Institutional

US09251T5092

MALOX

20210103

T

T

1,241

16

Fidelity Low-Priced Stock Fund

US3163453059

FLPSX

20210103

T

799

0

BlackRock Liquidity Treasury Trust Fund;Inst

US09248U5517

TTTXX

20210103

T

63

0

DFA Five-Year Global Fixed Income Portfolio;Inst

US2332038841

DFGBX

20210103

T

350

0

Federated Hermes US Treasury Cash Reserves;Inst

US60934N6821

UTIXX

20210103

T

55

0

Oakmark International Fund;Investor

US4138382027

OAKIX

20210103

T

80

0

Advanced Srs T Rowe Price Asset Allocation Port

US00767H4939

20210103

T

2,787

0

FPA Crescent Fund;Inst

US30254T7596

FPACX

20210103

T

138

0

Franklin Income Fund;C

US3534968058

FCISX

20210103

T

T

302

1

MFS Value Fund;I

US5529836943

MEIIX

20210103

T

76

0

Goldman Sachs FS Treasury Instruments Fd;Inst

US38142B5003

FTIXX

20210103

T

67

0

Schwab S&P 500 Index Fund

US8085098551

SWPPX

20210103

T

508

0

Templeton Global Bond Fund;Advisor

US8802084009

TGBAX

20210103

T

T

82

1

First Eagle Global Fund;I

US32008F6060

SGIIX

20210103

T

T

225

1

iShares Core S&P 500 ETF

US4642872000

IVV

20210103

T

T

507

1

Vanguard 500 Index Fund;Admiral

US9229087104

VFIAX

20210103

T

510

0

Vanguard Total Stock Market Index Fund;Admiral

US9229087286

VTSAX

20210103

T

3,370

0

SPDR S&P MidCap 400 ETF

US78467Y1073

MDY

20210103

T

400

0

SPDR Dow Jones Industrial Average ETF Trust

US78467X1090

DIA

20210103

T

31

0

Health Care Select Sector SPDR Fund

US81369Y2090

XLV

20210103

T

T

64

1

Consumer Discretionary Select Sector SPDR Fund

US81369Y4070

XLY

20210103

T

T

62

1

Energy Select Sector SPDR Fund

US81369Y5069

XLE

20210103

T

T

28

1

Financial Select Sector SPDR Fund

US81369Y6059

XLF

20210103

T

T

66

1

Technology Select Sector SPDR Fund

US81369Y8030

XLK

20210103

T

T

74

1

Invesco QQQ Trust Series 1

US46090E1038

QQQ

20210103

T

104

0

Dodge & Cox International Stock Fund

US2562061034

DODFX

20210103

T

T

80

1

iShares MSCI EAFE ETF

US4642874659

EFA

20210103

T

T

897

1

JPMorgan 100% US Treasury Secs Mny Mkt Fd;Inst

US4812A28358

JTSXX

20210103

T

41

0

Invesco S&P 500 Eql Wght ETF

US46137V3574

RSP

20210103

T

T

507

1

JPMorgan 100% US Treasury Secs Mny Mkt Fd;Capital

US4812A03757

CJTXX

20210103

T

41

0

Invesco Developing Markets Fund;Y

US00143W8753

ODVYX

20210103

T

T

72

3

SPDR S&P Dividend ETF

US78464A7634

SDY

20210103

T

T

118

1

DFA International Core Equity Portfolio;Inst

US2332033719

DFIEX

20210103

T

4,888

0

Edgewood Growth Fund;Institutional

US0075W07594

EGFIX

20210103

T

T

23

1

Advanced Series Prudential Growth Allocation Port

US00767H7585

20210103

T

T

2,400

3

MFS Value Fund;R6

US55273H3536

MEIKX

20210103

T

76

0

Requirement

  1. Write a function read_stock() that …

    • takes a filename parameter

    • reads the CSV (using a csv.DictReader to automatically absorb the header line of the CSV file into dictionary keys), and thereby converts the fields as follows,

      • FUND_NAME: str

      • FUND_ISIN: str

      • FUND_TICKER: str

      • AS_OF_DATE: datetime

      • FUND_IN_UNIVERSE: bool (interpreting T and F straightforwardly)

      • FUND_HOLDINGS_COUNT: float

      • FUND_HOLDING_FUNDS_COUNT: float

    • returns a list of dictionaries (better yet: an iterable).

  2. Put that function in a module, say stock.py

  3. Write an executable program, say read-stock.py, that is called like follows,

    $ python read-stock.py stock.csv
    ...
    

    It then writes the returned dictionaries/record to stdout in the most beautiful way you can come up with.

Footnotes

Dependencies

cluster_python Python Programming cluster_python_basics Python: The Language Fundamentals cluster_python_advanced Python: More Language Features cluster_python_misc Python: Miscellaneous Topics cluster_python_exercises Exercises cluster_python_exercises_csv CSV and Databases python_basics_python_0139_commandline_argv Commandline Arguments (sys.argv) python_basics_python_0125_running Running Python Programs python_basics_python_0139_commandline_argv->python_basics_python_0125_running python_basics_python_0130_syntax_etc Syntax etc. python_basics_python_0139_commandline_argv->python_basics_python_0130_syntax_etc python_basics_python_0140_variables Variables python_basics_python_0140_variables->python_basics_python_0130_syntax_etc python_basics_python_0120_helloworld Hello World python_basics_python_0110_blahblah Blahblah python_basics_python_0120_helloworld->python_basics_python_0110_blahblah python_basics_python_0500_files File I/O python_basics_python_0220_for for Loops python_basics_python_0500_files->python_basics_python_0220_for python_misc_encoding Encoding python_basics_python_0500_files->python_misc_encoding python_basics_python_0270_functions Functions python_basics_python_0270_functions->python_basics_python_0140_variables python_basics_python_0150_datatypes_overview Datatypes python_basics_python_0270_functions->python_basics_python_0150_datatypes_overview python_basics_python_0300_strings More About Strings python_basics_python_0250_refs_flat_deep_copy References, (Im)mutability python_basics_python_0300_strings->python_basics_python_0250_refs_flat_deep_copy python_basics_python_0300_strings->python_basics_python_0150_datatypes_overview python_basics_python_0200_sequential_types Sequential Datatypes python_basics_python_0300_strings->python_basics_python_0200_sequential_types python_basics_python_0150_datatypes_overview_compound Compound Datatypes python_basics_python_0150_datatypes_overview_compound->python_basics_python_0150_datatypes_overview python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0140_variables python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0150_datatypes_overview_compound python_basics_python_0250_refs_flat_deep_copy->python_basics_python_0150_datatypes_overview python_basics_python_0150_datatypes_overview->python_basics_python_0140_variables python_basics_python_0225_range The range Function python_basics_python_0225_range->python_basics_python_0200_sequential_types python_basics_python_0225_range->python_basics_python_0220_for python_basics_python_0125_running->python_basics_python_0120_helloworld python_basics_python_0200_sequential_types->python_basics_python_0150_datatypes_overview_compound python_basics_python_0160_boolean Boolean python_basics_python_0160_boolean->python_basics_python_0150_datatypes_overview python_basics_python_0193_while while Loops python_basics_python_0193_while->python_basics_python_0160_boolean python_basics_python_0170_if The if Statement python_basics_python_0193_while->python_basics_python_0170_if python_basics_python_0170_if->python_basics_python_0160_boolean python_basics_python_0320_strings_methods Miscellaneous String Methods python_basics_python_0320_strings_methods->python_basics_python_0300_strings python_basics_python_0220_for->python_basics_python_0200_sequential_types python_basics_python_0220_for->python_basics_python_0193_while python_basics_python_0130_syntax_etc->python_basics_python_0120_helloworld python_advanced_python_1010_generators_yield Iteration, Generators, And yield python_advanced_python_1010_generators_yield->python_basics_python_0270_functions python_advanced_python_1010_generators_yield->python_basics_python_0225_range python_advanced_python_1010_generators_yield->python_basics_python_0200_sequential_types python_advanced_python_1010_generators_yield->python_basics_python_0220_for python_advanced_modules Modules and Packages python_misc_csv CSV Files python_misc_csv->python_basics_python_0500_files python_misc_csv->python_basics_python_0150_datatypes_overview_compound python_misc_csv->python_basics_python_0220_for python_misc_encoding->python_basics_python_0150_datatypes_overview python_misc_encoding->python_basics_python_0320_strings_methods python_exercises_csv_csv_module Exercise: Read CSV File python_exercises_csv_csv_module->python_basics_python_0139_commandline_argv python_exercises_csv_csv_module->python_advanced_python_1010_generators_yield python_exercises_csv_csv_module->python_advanced_modules python_exercises_csv_csv_module->python_misc_csv