예제 #1
0
def parse_Merimee_csv(csv_file):
    """Return the Mérimée database (as a dictionary) from the given CSV file."""
    file_handler = codecs.open(csv_file, 'r', 'latin-1')
    dataset = {}
    csvReader = UnicodeCSV.unicode_csv_dictreader(file_handler,
                                                  delimiter=';')
    try:
        for row in csvReader:
            merimee_id = row['REF']
            dataset[merimee_id] = handle_Merimee_csv_row(row)
    except csv.Error, e:
        sys.exit('file %s, line %d: %s' % (csv_file,
                                           csvReader.line_num, e))
예제 #2
0
 def retrieve_metadata_from_csv(self, csv_file, delimiter=','):
     """Retrieve metadata from the given CSV file."""
     file_handler = codecs.open(csv_file, 'r', 'utf-8')
     csvReader = UnicodeCSV.unicode_csv_dictreader(file_handler,
                                                   delimiter=delimiter)
     try:
         for row in csvReader:
             metadata_record = self.handle_record(row)
             self.records.append(metadata_record)
             self.fields.update(metadata_record.get_field_names())
     except csv.Error, e:
         sys.exit('file %s, line %d: %s' % (csv_file,
                                            csvReader.line_num, e))
예제 #3
0
def read_csv(csv_file, delimiter):
    """Read the CSV file and return each line."""
    file_handler = codecs.open(csv_file, 'r', 'utf-8')
    return UnicodeCSV.unicode_csv_dictreader(file_handler,
                                             delimiter=delimiter)