Book Image

Python for Finance

By : Yuxing Yan
Book Image

Python for Finance

By: Yuxing Yan

Overview of this book

Table of Contents (20 chapters)
Python for Finance
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Retrieving data from an external text file


When retrieving data from an external data file, the variable generated will be a list.

>>>f=open("c:\\data\\ibm.csv","r")
>>>data=f.readlines()
>>>type(data)
<class 'list'>

The first few lines of the input file are shown in the following lines of code. In Chapter 7, Visual Finance via Matplotlib, we will discuss how to download this input file from Yahoo! Finance.

>>>Date,Open,High,Low,Close,Volume,Adj Close
2013-07-26,196.59,197.37,195.00,197.35,2485100,197.35
2013-07-25,196.30,197.83,195.66,197.22,3014300,197.22
2013-07-24,195.95,197.30,195.86,196.61,2957900,196.61
2013-07-23,194.21,196.43,194.10,194.98,2863800,194.98
2013-07-22,193.40,195.79,193.28,194.09,3398000,194.09
2013-07-19,197.91,197.99,193.24,193.54,6997600,193.54

After we generate a variable called stock, we can view its first two observations as follows:

>>>data[1]
'2013-07-26,196.59,197.37,195.00,197.35,2485100,197.35\n'
>&gt...