forked from Apress/practical-ml-w-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_csv.py
More file actions
93 lines (69 loc) · 2.18 KB
/
read_csv.py
File metadata and controls
93 lines (69 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# -*- coding: utf-8 -*-
"""
Created on Wed May 10 19:51:47 2017
@author: Raghav Bali
"""
"""
This script showcases methods to extract data from CSVs:
+ csv containing delimiter separated values
+ csv containing tabular data
using csv and pandas packages
``Execute``
$ python read_csv.py
"""
import csv
import pandas as pd
from pprint import pprint
def print_basic_csv(file_name, delimiter=','):
"""This function extracts and prints csv content from given filename
Details: https://docs.python.org/2/library/csv.html
Args:
file_name (str): file path to be read
delimiter (str): delimiter used in csv. Default is comma (',')
Returns:
None
"""
csv_rows = list()
csv_attr_dict = dict()
csv_reader = None
# read csv
csv_reader = csv.reader(open(file_name, 'r'), delimiter=delimiter)
# iterate and extract data
for row in csv_reader:
print(row)
csv_rows.append(row)
# prepare attribute lists
for col in csv_rows[0]:
csv_attr_dict[col]=list()
# iterate and add data to attribute lists
for row in csv_rows[1:]:
csv_attr_dict['sno'].append(row[0])
csv_attr_dict['fruit'].append(row[1])
csv_attr_dict['color'].append(row[2])
csv_attr_dict['price'].append(row[3])
# print the result
print("\n\n")
print("CSV Attributes::")
pprint(csv_attr_dict)
def print_tabular_data(file_name,delimiter=","):
"""This function extracts and prints tabular csv content from given filename
Details: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html
Args:
file_name (str): file path to be read
delimiter (str): delimiter used in csv. Default is comma ('\t')
Returns:
None
"""
df = pd.read_csv(file_name,sep=delimiter)
print(df)
if __name__=='__main__':
print("\n\n")
print("*"*30)
print("Contents of sample csv file:")
print("*"*30)
print_basic_csv(r'tabular_csv.csv')
print("\n\n")
print("*"*30)
print("Contents of a tabular csv file:")
print("*"*30)
print_tabular_data(r'tabular_csv.csv')