Changeset 4162
- Timestamp:
- 24 May 2009, 10:13:21 (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-rewrite/src/waeup/university/facultycontainer.txt
r4080 r4162 69 69 like. It might be a string of integers, a name or whatever; you cannot 70 70 know before. 71 72 Importing from CSV files 73 ------------------------ 74 75 We create a facultycontainer, which we want to populate with some CSV 76 data afterwards: 77 78 >>> from waeup.university.facultycontainer import FacultyContainer 79 >>> mycontainer = FacultyContainer() 80 81 Next, we create a CSV file we want to import: 82 83 >>> open('myfaculties.csv', 'wb').write( 84 ... """code,review_state,title,title_prefix 85 ... AGR,static,Agriculture,faculty 86 ... ART,static,Arts,faculty 87 ... DEN,static,Dentistry,faculty 88 ... """) 89 90 We get a suitable importer by asking for an adapter to 91 IWAeUPCSVExporter (see section above for details): 92 93 >>> from waeup.interfaces import IWAeUPCSVImporter 94 >>> importer = IWAeUPCSVImporter(mycontainer) 95 >>> importer 96 <waeup.university.facultycontainer.FacultyCSVImporter object at 0x...> 97 98 This importer complies with the IWAeUPCSVImporter interface: 99 100 >>> from waeup.university.facultycontainer import FacultyCSVImporter 101 >>> IWAeUPCSVImporter.implementedBy(FacultyCSVImporter) 102 True 103 104 >>> from zope.interface.verify import verifyClass 105 >>> verifyClass(IWAeUPCSVImporter, FacultyCSVImporter) 106 True 107 108 The container is still empty: 109 110 >>> len(mycontainer) 111 0 112 113 Now we can easily import the data: 114 115 >>> importer.doImport('myfaculties.csv') 116 117 The container is now filled: 118 119 >>> len(mycontainer) 120 3 121 122 >>> print [(x.code, x.title) for x in list(mycontainer.values())] 123 [('AGR', 'Agriculture'), ('ART', 'Arts'), ('DEN', 'Dentistry')] 124 125 126 We can validate data of CSV files without actually importing 127 them. This can help for instance while checking uploaded files: 128 129 >>> importer.validate('myfaculties.csv') 130 True 131 132 Clean up: 133 134 >>> import os 135 >>> os.unlink('myfaculties.csv')
Note: See TracChangeset for help on using the changeset viewer.