Read data from .csv file with Python

Sample code that reads data from .csv file with Python

import csv
csvfile = 'file address'
file_data = open(csvfile, 'rb')
b = csv.reader(file_data)
def read_csv_tolist(filename):
    import csv
    f = filename
    f_open = open(f, 'rb')
    d = csv.reader(f_open)
    data_list = []
    for i in d:
        data_list.append(i)
    return data_list     # [row][data] 

def list_to_float(data_list):   # [a, b, c, ...]
    r_list = []
    if data_list[-1] == '\n':
        length = len(data_list)-1
    else:
        length = len(data_list)
    for i in range(0, length):
        r_list.append(float(data_list[i]))
    return r_list
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s