Commit 88074bd0 authored by sainath taidalwar's avatar sainath taidalwar

Upload New File

parent b84536cc
Pipeline #21820 failed
with open ('Sample.csv', 'r') as f:
for line in f:
line = line.strip()
row = line.split(',')
print(row)
from tabulate import tabulate
import csv
myrows=[]
records=[]
with open('Sample.csv','r') as f:
r = csv.DictReader(f)
for row in r:
records.append(row)
print(records)
from collections import OrderedDict
def select_cols(records, columns):
new_records = []
for record in records:
new_record = OrderedDict()
for c in columns:
new_record[c] = record[c]
new_records.append(new_record)
return new_records
select_cols(records,['Names','CGPA'])
def print_records_fancy(records):
xyz = list(records[0].keys())
recs = []
for record in records:
recs.append(list(record.values()))
print(tabulate(recs, headers=xyz))
student = select_cols(records, ['Names', 'CGPA'])
print_records_fancy(student)
def check_constraints(record, constraints):
all_okay = True
for col, cs in constraints.items():
csop = cs[0]
csval = cs[1]
if csop in ['EQ','GT','LT']:
if csop == 'EQ' and record[col] != csval:
return False
elif csop == 'GT' and float(record[col]) <= float(csval):
return False
elif csop == 'LT' and float(record[col]) >= float(csval):
return False
else:
return False
return all_okay
def filter_records(columns=[], constraints={}):
f_records = []
if len(columns) == 0:
columns = list(records[0].keys())
for record in records:
new_record = OrderedDict()
if check_constraints(record, constraints):
for col in columns:
new_record[col] = record[col]
f_records.append(new_record)
return f_records
def len_records(records):
rec_len = len(records)
return [OrderedDict({'Length': rec_len})]
def print_records(records):
headers = list(records[0].keys())
print("\t".join(headers))
for record in records:
print("\t".join(list(record.values())))
def print_records_fancy(records):
headers = list(records[0].keys())
recs = []
for record in records:
recs.append(list(record.values()))
print(tabulate(recs, headers=headers))
print_records_fancy(filter_records( constraints={'College': ['EQ', 'CMREC']}) )
def sum_records(records, columns):
sums = OrderedDict()
for col in columns:
sums[col] = 0
for record in records:
for col in columns:
sums[col] += float(record[col])
return [sums]
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment