#This program reads the data in tdata.csv and creates lists of different object types import csv rowlist, dictlist, tuplelist, setlist = [], [], [], [] #Read the data from the file to be read reader = csv.reader(open('tdata.csv', 'r'), delimiter=',') #Process each row for row in reader: rowlist.append(list(row)) #builds a list of records; each record is a list itself print('Here is the data in one statement:', rowlist) print('Here is the data in a loop:') for r in rowlist: print(r) print('Here is the data in dictionaries:') for r in rowlist: D = dict(zip(['state', 'yearinUS', 'pop'], r)) dictlist.append(D) #builds a list of records; each record is a dictionary print(dictlist) """ A good test: make the prior loop more general by putting column names in the first row of the file. """ print('Here is the data in tuples:') for r in rowlist: T = tuple(r) tuplelist.append(T) #builds a list of records; each record is a tuple print(tuplelist) print('Here is the data in sets:') for r in rowlist: S = set(r) #another way: for i in l: S.add(i) setlist.append(S) #builds a list of records; each record is a set print(setlist) outfile = open('tdataout.txt','w') line = 'Data in: ' + str(rowlist) + '\n' outfile.write(line) line = 'Dictionaries: ' + str(dictlist) + '\n' outfile.write(line) line = 'Tuples: ' + str(tuplelist) + '\n' outfile.write(line) line = 'Sets: ' + str(setlist) + '\n' outfile.write(line)