| OLD | NEW |
| 1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
| 2 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. | 2 # copyright 2003-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
| 3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr | 3 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
| 4 # | 4 # |
| 5 # This file is part of logilab-common. | 5 # This file is part of logilab-common. |
| 6 # | 6 # |
| 7 # logilab-common is free software: you can redistribute it and/or modify it unde
r | 7 # logilab-common is free software: you can redistribute it and/or modify it unde
r |
| 8 # the terms of the GNU Lesser General Public License as published by the Free | 8 # the terms of the GNU Lesser General Public License as published by the Free |
| 9 # Software Foundation, either version 2.1 of the License, or (at your option) an
y | 9 # Software Foundation, either version 2.1 of the License, or (at your option) an
y |
| 10 # later version. | 10 # later version. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 >>> rec = readDbf('test.dbf') | 23 >>> rec = readDbf('test.dbf') |
| 24 >>> for line in rec: | 24 >>> for line in rec: |
| 25 >>> print line['name'] | 25 >>> print line['name'] |
| 26 | 26 |
| 27 | 27 |
| 28 :date: 13/07/2007 | 28 :date: 13/07/2007 |
| 29 | 29 |
| 30 http://www.physics.ox.ac.uk/users/santoso/Software.Repository.html | 30 http://www.physics.ox.ac.uk/users/santoso/Software.Repository.html |
| 31 page says code is "available as is without any warranty or support". | 31 page says code is "available as is without any warranty or support". |
| 32 """ | 32 """ |
| 33 from __future__ import print_function |
| 33 | 34 |
| 34 import struct | 35 import struct |
| 35 import os, os.path | 36 import os, os.path |
| 36 import sys | 37 import sys |
| 37 import csv | 38 import csv |
| 38 import tempfile | 39 import tempfile |
| 39 | 40 |
| 40 from six.moves import range | 41 from six.moves import range |
| 41 | 42 |
| 42 class Dbase: | 43 class Dbase: |
| (...skipping 29 matching lines...) Expand all Loading... |
| 72 idx = 0 | 73 idx = 0 |
| 73 for item in lst: | 74 for item in lst: |
| 74 id = ids[idx] | 75 id = ids[idx] |
| 75 result[id] = item | 76 result[id] = item |
| 76 idx += 1 | 77 idx += 1 |
| 77 return result | 78 return result |
| 78 | 79 |
| 79 def open(self, db_name): | 80 def open(self, db_name): |
| 80 filesize = os.path.getsize(db_name) | 81 filesize = os.path.getsize(db_name) |
| 81 if filesize <= 68: | 82 if filesize <= 68: |
| 82 raise IOError, 'The file is not large enough to be a dbf file' | 83 raise IOError('The file is not large enough to be a dbf file') |
| 83 | 84 |
| 84 self.fdb = open(db_name, 'rb') | 85 self.fdb = open(db_name, 'rb') |
| 85 | 86 |
| 86 self.memo_file = '' | 87 self.memo_file = '' |
| 87 if os.path.isfile(db_name[0:-1] + 't'): | 88 if os.path.isfile(db_name[0:-1] + 't'): |
| 88 self.memo_file = db_name[0:-1] + 't' | 89 self.memo_file = db_name[0:-1] + 't' |
| 89 elif os.path.isfile(db_name[0:-3] + 'fpt'): | 90 elif os.path.isfile(db_name[0:-3] + 'fpt'): |
| 90 self.memo_file = db_name[0:-3] + 'fpt' | 91 self.memo_file = db_name[0:-3] + 'fpt' |
| 91 | 92 |
| 92 if self.memo_file: | 93 if self.memo_file: |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 145 self.fmemo.close() | 146 self.fmemo.close() |
| 146 | 147 |
| 147 def get_numrecords(self): | 148 def get_numrecords(self): |
| 148 return self.num_records | 149 return self.num_records |
| 149 | 150 |
| 150 def get_record_with_names(self, rec_no): | 151 def get_record_with_names(self, rec_no): |
| 151 """ | 152 """ |
| 152 This function accept record number from 0 to N-1 | 153 This function accept record number from 0 to N-1 |
| 153 """ | 154 """ |
| 154 if rec_no < 0 or rec_no > self.num_records: | 155 if rec_no < 0 or rec_no > self.num_records: |
| 155 raise Exception, 'Unable to extract data outside the range' | 156 raise Exception('Unable to extract data outside the range') |
| 156 | 157 |
| 157 offset = self.header['Record Size'] * rec_no | 158 offset = self.header['Record Size'] * rec_no |
| 158 data = self.db_data[offset:offset+self.row_len] | 159 data = self.db_data[offset:offset+self.row_len] |
| 159 record = self._assign_ids(struct.unpack(self.row_format, data), self.row
_ids) | 160 record = self._assign_ids(struct.unpack(self.row_format, data), self.row
_ids) |
| 160 | 161 |
| 161 if self.memo_file: | 162 if self.memo_file: |
| 162 for key in self.fields: | 163 for key in self.fields: |
| 163 field = self.fields[key] | 164 field = self.fields[key] |
| 164 f_type = field['Field Type'] | 165 f_type = field['Field Type'] |
| 165 f_name = field['Field Name'] | 166 f_name = field['Field Name'] |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 rec = [] | 221 rec = [] |
| 221 for i in range(0, num): | 222 for i in range(0, num): |
| 222 record = db.get_record_with_names(i) | 223 record = db.get_record_with_names(i) |
| 223 rec.append(record) | 224 rec.append(record) |
| 224 db.close() | 225 db.close() |
| 225 return rec | 226 return rec |
| 226 | 227 |
| 227 if __name__=='__main__': | 228 if __name__=='__main__': |
| 228 rec = readDbf('dbf/sptable.dbf') | 229 rec = readDbf('dbf/sptable.dbf') |
| 229 for line in rec: | 230 for line in rec: |
| 230 print '%s %s' % (line['GENUS'].strip(), line['SPECIES'].strip()) | 231 print('%s %s' % (line['GENUS'].strip(), line['SPECIES'].strip())) |
| OLD | NEW |