Python import table data from Mac .numbers file

pappusenpai picture pappusenpai · Dec 29, 2017 · Viewed 12.6k times · Source

I'm new to Python and I'm trying to crunch some numbers. Sample attached: Open High Low Close Sample Data

I have tested a few variations of importing data but failed. Really appreciate some advise. Thanks!

path = 'Data/Price.numbers'
with open(path) as file:    
file.readline()
for line in file:
    values = map(float, line.split())
    test.append(values)

Key Objectives:

1) Efficiently store the table data in a format that I can easily manipulate and apply calculations > I'm thinking of a Dict{} > Any comments?

2) Optimised for quick calculations as I need to crunch data for multiple securities > I estimate about 1,000,000 to 2,000,000 datapoint.

Again, appreciate any advise to do this better.

Answer

Gregorio Aiolfi picture Gregorio Aiolfi · Dec 29, 2017

You can use the pandas library. Take a look at the documentation here

For example, if you save the file as a csv, with comma as delimiter you can use:

import pandas as pd
df = pd.read_csv('name of the file.csv', sep=',')

In case you get the encoding error you can try:

import pandas as pd
df = pd.read_csv('name of the file.csv', sep=',', encoding='latin-1')