-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.py
161 lines (126 loc) · 4.74 KB
/
script.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
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
155
156
157
158
159
160
161
import csv
def readalldata():
csv_file = open('gsquarterly_december-2020-revised.csv','r')
csv_reader = csv.reader(csv_file, delimiter=',')
next(csv_reader)
for row in csv_reader:
serial_no, time_ref, account, code, country_c, product_t, value, status = row
print(row)
def searchdata():
def searchbyserial_no():
serial_no = input('Enter serial no for search:\n')
csv_file = csv.reader(open('gsquarterly_december-2020-revised.csv','r'))
for row in csv_file:
if serial_no == row[0]:
print(row)
def searchbytime_ref():
time_ref = input('Enter time_ref for search:\n')
csv_file = csv.reader(open('gsquarterly_december-2020-revised.csv','r'))
for row in csv_file:
if time_ref == row[1]:
print(row)
def searchbyaccount():
account = input('Enter account for search:\n')
csv_file = csv.reader(open('gsquarterly_december-2020-revised.csv','r'))
for row in csv_file:
if account == row[2]:
print(row)
def searchbycode():
code = input('Enter code for search:\n')
csv_file = csv.reader(open('gsquarterly_december-2020-revised.csv','r'))
for row in csv_file:
if code == row[3]:
print(row)
def searchbycountry_c():
country_c = input('Enter country code for search:\n')
csv_file = csv.reader(open('gsquarterly_december-2020-revised.csv','r'))
for row in csv_file:
if country_c == row[4]:
print(row)
def searchbyproduct_t():
product_t = input('Enter product type for search:\n')
csv_file = csv.reader(open('gsquarterly_december-2020-revised.csv','r'))
for row in csv_file:
if product_t == row[5]:
print(row)
def searchbyvalue():
value = input('Enter value for search:\n')
csv_file = csv.reader(open('gsquarterly_december-2020-revised.csv','r'))
for row in csv_file:
if value == row[6]:
print(row)
def searchbystatus():
status = input('Enter status for search:\n')
csv_file = csv.reader(open('gsquarterly_december-2020-revised.csv', 'r'))
for row in csv_file:
if status == row[7]:
print(row)
print('Enter 1 to search by serial no.')
print('Enter 2 to search by time reference.')
print('Enter 3 to search by account.')
print('Enter 4 to search by code.')
print('Enter 5 to search by country code.')
print('Enter 6 to search by product type.')
print('Enter 7 to search by value.')
print('Enter 8 to search by status.')
src = int(input('Enter here: '))
if src == 1:
searchbyserial_no()
elif src == 2:
searchbytime_ref()
elif src == 3:
searchbyaccount()
elif src == 4:
searchbycode()
elif src == 5:
searchbycountry_c()
elif src == 6:
searchbyproduct_t()
elif src == 7:
searchbyvalue()
elif src == 8:
searchbystatus()
else:
print('Sorry, invalid input')
def sortdata():
def sort_csv(n, val):
sorted_list = []
heading = []
with open(r'gsquarterly_december-2020-revised.csv','r') as file:
read = csv.reader(file, delimiter=',')
read_list = list(read)
heading = read_list[0]
print(heading)
if val == 0:
sorted_list = sorted(read_list[1:], key=lambda x: x[n])
elif val == 1:
sorted_list = sorted(read_list[1:], key=lambda x: float(x[n]))
for each in sorted_list:
print(each)
with open(r'gsquarterly_december-2020-revised.csv','w', newline='') as file:
writer = csv.writer(file)
writer.writerow(heading)
writer.writerow(sorted_list)
with open(r'gsquarterly_december-2020-revised.csv','r') as file:
read = csv.reader(file, delimiter=',')
heading = list(read)[0]
print("Select one of the column to sort accordingly:")
i = 0
for each in heading:
print(i, ":", each)
i += 1
n = int(input("Enter your choice:"))
value = int(input("Specify the column is:\n0 : string\n1: integer\n:"))
sort_csv(n, value)
print('Enter 1 for read all data.')
print('Enter 2 for search data.')
print('Enter 3 for sort data.')
rss= int(input('Enter here: '))
if rss == 1:
readalldata()
elif rss == 2:
searchdata()
elif rss == 3:
sortdata()
else:
print('Sorry, invalid input')