Import CSV Analyse and Parse contents

 Reading a csv file:

import csv

path1='example.txt'

#Reading this into a list

reader=list(csv.reader(open(path1), delimiter='\t'))

#The reader object takes into one list element

#treat this list element like a string and split

print(reader[1][0].split(' '))

#It breaks into:

['', '2', '98', '3110111288', '205017830', '2', 'firstname', 'middlename', 'lastname', 'M', 'COMN', '1103:GSMC', 'MUMBAI']
 
# to read individual elements
print(recordreader[6]) returns firstname 
 
#Now we can perform operation which splits the characters into individual elements and perform 
operation on them
for rec in reader:
# print(rec)
print(len(str(rec)))
for i in range(0,4):
recordreader=reader[i][0].split(' ')
for j in range(0,9):
print(recordreader[j])
 #Getting these records into a 2-D list-
for rec in reader:
# print(rec)
print(len(rec))
for i in range(0,4):
recordreader=reader[i][0].split(' ')
print(len(recordreader))
col=[]
for j in range(0,len(recordreader)):

col.append(recordreader[j])
#print(recordreader[j])
print(col)
irec.append(col) 
 
##Check each record for M or F in the 8 and 9 positions and if present then insert space 
at each place
for j in range(0,40000):
try:
#print('Initial',irec[j])
#print(irec[3][8])
for i in range(8,10):
if (irec[j][i]=='M' or irec[j][i]=='F'):
#print(j,i)
irec[j].insert(i, ' ')
except:
continue
#else:
#print('Nothing')
#print('Changedto', irec[j]) 
 
Text to blink here

Comments

Popular posts from this blog