forked from JasonMDev/learning-python-predictive-analytics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadDatasetByOpenMethod.py
57 lines (45 loc) · 1.41 KB
/
readDatasetByOpenMethod.py
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
# -*- coding: utf-8 -*-
"""
Created on Wed Apr 27 19:58:48 2016
@author: jasonm_dev
python2 uses next()
python3 uses readline()
"""
path = '/home/jasonm_dev/coding/learning-python-predictive-analytics/Ch02'
filename = 'Customer Churn Model.txt'
fullpath = path+'/'+filename
# Open file in read mode.
data=open(fullpath,'r')
# readline() method:
# -> It navigates the computer memory to the line next to the header.
# strip() method:
# -> Removes all the trailing and leading blank spaces from the line
# split() method:
# -> Method breaks down a line into chunks separated by the argument provided
cols=data.readline().strip().split(',')
no_cols=len(data.readline().strip().split(','))
counter=0
main_dict={}
# Key: Column names
# Value: Values of columns.
for col in cols:
main_dict[col]=[]
for line in data:
values = line.strip().split(',')
for i in range(len(cols)):
main_dict[cols[i]].append(values[i])
counter += 1
#print ("The dataset has %d rows and %d columns") % (counter,no_cols)
print ('The dataset has ',counter,' rows and ',no_cols,' columns')
# Convert dataset to a dataframe similar pandas raed_csv
import pandas as pd
df=pd.DataFrame(main_dict)
print (df.head(10))
filename_csv = 'Write.csv'
filename_xls = 'Write.xls'
fullpath_csv = path+'/'+filename_csv
fullpath_xls = path+'/'+filename_xls
# Write to CSV file.
df.to_csv(fullpath_csv)
# Write to xls file
df.to_excel(fullpath_xls)