-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread.py
More file actions
154 lines (126 loc) · 3.42 KB
/
Copy pathread.py
File metadata and controls
154 lines (126 loc) · 3.42 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Some data manipulation techniques without numpy and pandas!! ---> Important concepts.
import sys
# Reading Data from a CSV file ---------- Without Pandas & Numpy
delimit = ','
infile = open('C:\\Users\\ankes\\Documents\\Python Scripts\\newfile.csv', 'r')
import csv
newdata = []
rows = 0
for row in csv.reader(infile,delimiter=delimit):
rows += 1
if rows < 102:
newdata.append(row)
#print(newdata)
#infile.close
rows = -1
# To find total list items in vector infile
infile.seek(0)
ncol = len(next(infile))
print("Total number of data items in list vector: ",ncol)
#print(newdata)
print(newdata[4][5])
# Copying Data from source CSV file to new CSV file
rows = -1
with open('dribble.csv','w',newline = '') as newfile:
writer = csv.writer(newfile)
for row in newdata:
rows += 1
if rows < 101:
writer.writerow(newdata[rows])
# Modify: Adding column or data to any row in data frame ----- Without Pandas & Numpy
rows = 0
with open('dribble.csv','w',newline = '') as modfile:
moder = csv.writer(modfile)
for row in newdata:
if rows == 0:
row.append("Name")
rows += 1
moder.writerow(row)
#if rows == 1:
# break
# Counting Rows & Columns in CSV --------------------------- Without Pandas & Numpy
rows = 0
colcount = 0
for row in newdata:
rows += 1
if colcount < 1:
try:
data = newdata[0][rows]
except IndexError as error:
print("Last Column Reached!!")
cols = rows
colcount = 1
print("Last Row Reached!!")
print("No. of columns in CSV: ", cols)
print("No. of rows in CSV: ", rows)
# Sorting Data -------- Bubble Sorting
# First covert str dataframe to int :
rows = 0
cols = 0
sort = []
ordsort = []
for row in newdata:
if rows < 100:
rows += 1
#print (newdata[rows][62])
sort.append(int(newdata[rows][62]))
print(sort)
# Then implement sorting algorithm :
item = 0
iter = 0
iterate = 1
counter = 0
#print (sort[item+1], sort[item])s
while iter < 4000 :
#print("MC")
if iterate > 0:
try:
trye = sort[item+1]
except IndexError as error:
item = 0
counter += 1
if sort[item] < sort[item+1] :
iterate = 1
temp = sort[item]
sort[item] = sort[item + 1]
sort[item + 1] = temp
#print(sort[item])
item += 1
iter += 1
print("\n\n",sort)
# Optimized bubble sort algorithm for implementation
item = 1
for i in sort:
try:
temp = sort[item]
except IndexError as error:
print ("Length of List: ", item)
item += 1
lenoflist = item - 2
item = 0
ite = 0
iterate = 0
incount = 1
count = 0
while incount > 0:
count += 1
ite = 0
incount = 0
if iterate > 0:
try:
temp = sort[item+1]
except IndexError as error:
item = 0
print("Error")
while ite < lenoflist:
if sort[ite] < sort[ite+1]:
incount = 1
temp = sort[ite]
sort[ite] = sort[ite+1]
sort[ite+1] = temp
ite += 1
item = 0
iterate = 1
print ("Sorted List: ",sort)
print ("Iterator run: ",count, "times")
infile.close