Python for Finance: Analyze Big Financial Data

(Elle) #1

The actual data is then written row by row, merging the date-time information with the


(pseudo)random numbers:


In  [ 34 ]: for t_, (no1,   no2,    no3,    no4,    no5)    in zip(t,   a):
s = ‘%s,%f,%f,%f,%f,%f\n’ % (t_, no1, no2, no3, no4, no5)
csv_file.write(s)
csv_file.close()
In [ 35 ]: ll $path*
Out[35]: -rw-r—r— 1 root 337664 28. Sep 15:16 /flash/data/data.csv

The other way around works quite similarly. First, open the now-existing CSV file. Second,


read its content line by line using the readline method of the file object:


In  [ 36 ]: csv_file    =   open(path   +   ‘data.csv’, ‘r’)        #   open    file    for reading
In [ 37 ]: for i in range( 5 ):
print csv_file.readline(),
Out[37]: date,no1,no2,no3,no4,no5
2014-01-01 00:00:00,1.381035,-1.123613,1.062245,-1.399746,-0.737369
2014-01-01 01:00:00,0.149965,0.966987,1.839130,0.563322,0.056906
2014-01-01 02:00:00,-0.950360,0.477881,1.863646,-1.915203,-0.300522
2014-01-01 03:00:00,-0.503429,-0.895489,-0.240227,-0.327176,0.123498

You can also read all the content at once by using the readlines method:


In  [ 38 ]: csv_file    =   open(path   +   ‘data.csv’, ‘r’)
content = csv_file.readlines()
for line in content[: 5 ]:
print line,
Out[38]: date,no1,no2,no3,no4,no5
2014-01-01 00:00:00,1.381035,-1.123613,1.062245,-1.399746,-0.737369
2014-01-01 01:00:00,0.149965,0.966987,1.839130,0.563322,0.056906
2014-01-01 02:00:00,-0.950360,0.477881,1.863646,-1.915203,-0.300522
2014-01-01 03:00:00,-0.503429,-0.895489,-0.240227,-0.327176,0.123498

We finish with some closing operations in this example:


In  [ 39 ]: csv_file.close()
!rm -f $path*

SQL Databases


Python can work with any kind of SQL database and in general also with any kind of NoSQL


database. One database that is delivered with Python by default is SQLite3. With it, the


basic Python approach to SQL databases can be easily illustrated:


[ 29 ]

In  [ 40 ]: import sqlite3 as sq3

SQL queries are formulated as string objects. The syntax, data types, etc. of course


depend on the database in use:


In  [ 41 ]: query   =   ‘CREATE TABLE   numbs   (Date   date,   No1 real,   No2 real)’

Open a database connection. In this case, we generate a new database file on disk:


In  [ 42 ]: con =   sq3.connect(path    +   ‘numbs.db’)

Then execute the query statement to create the table by using the method execute:


In  [ 43 ]: con.execute(query)
Out[43]: <sqlite3.Cursor at 0xb8a4490>

To make the query effective, call the method commit:


In  [ 44 ]: con.commit()

Now that we have a database file with a table, we can populate that table with data. Each


row consists of date-time information and two floats:

Free download pdf