In: Computer Science
need to write program on python, without using any other libraries like panda, numpy, etc.
here is google link on csv file https://drive.google.com/file/d/1O3cC9JAPVkXSrddTR6RocpSV8NMHrmRx/view?usp=sharing
There is a csv file, which is exel file, very big and what program should do:
- Read a CSV file 'annual.csv' enterprise into a data structure
- Count the number of rows and columns
- Determine if the data contains empty values (should search in all rows)
- Replace the empty values by 'NA' for strings, '0' for decimals and '0.0' for floats (in all rows)
- Transform all Upper case characters to Lower case characters (in every word)
- Transform all Lower case characters to Upper case characters (in every word in file)
- save back the 'repaired' array as csv and show edited version
- Print out the size of the data (number of rows, number of columns)
the file approzimately looks like this, but it is very huge
Code for above problem :-
import csv
# function to change the upper case to lower case or vice versa
def change(string):
new =''
for i in string:
# Checking for lowercase letter and converting to uppercase.
if (i.isupper()) == True:
new += (i.lower())
# Checking for uppercase letter and converting to lowercase.
elif (i.islower()) == True:
new += (i.upper())
# everything other than alphabet added as it is
else:
new += i
return new
# Data structure to store the imported data and edit
repaired = []
# reading from csv module
with open('annual.csv','r') as file:
data = csv.reader(file)
for row in data:
repaired.append(row)
# calculating no of rows
row_len = len(repaired)
# calculating no of columns
column_len = len(repaired[0])
# editing data
for i in range(1,row_len):
# Adding 0 to the cell that contains numerical values if empty
if repaired[i][8] == '':
repaired[i][8] = '0'
for j in range(column_len):
# Adding NA to the cell that contains string values if empty
if repaired[i][j]=='':
repaired[i][j]=='NA'
# calling function to change upper to lower or vice versa
string = repaired[i][j]
repaired[i][j] = change(string)
# creating another csv file of edited data
with open('repaired.csv','w',newline='') as file:
writer = csv.writer(file)
writer.writerows(repaired)
# print no of rows and columns
print("No. of Rows = ",row_len)
print("No. of Columns = ",column_len)
Screenshot for Code and Output :-
Screenshot of repaired.csv dataset:-