The goal is to organize the export of a table from postgresql to dbf format using python. For implementation, I use the dbfpy package.

from dbfpy import dbf, fields, header class Serializer(base.Serializer): def start_serialization(self): self.db = dbf.Dbf(self.stream, new=True) self.schema = [] self.dbfh = header.DbfHeader() def start_object(self, obj): if self.first: self.names = {} # Заполнение схемы данных - тип и название колонок for field in obj._meta.fields: if ('name' in self.options) and self.options['name']: self._name = self.options.pop('name') head_name = field.name.upper()[:10] else: self._name = False head_name = field.verbose_name[:10].encode('cp1251').upper() if 'name' in self.options: self.options.pop('name') self.schema.append((head_name, 'C', 64)) self.db.addField(*self.schema) # Здесь я пробую разными способами добавлять заголовок поля self.rec = self.db.newRecord() i = 0 for f in self.db.fieldNames: self.rec[i] = f i += 1 self.rec.store() self.rec = self.db.newRecord() def handle_field(self, obj, field): if getattr(obj, field.name) is not None: # возвращает название колонки if self._name: key = self.db.write(field.value_to_string(obj)) else: key = field.verbose_name[:10].encode('cp1251').upper() # возвращает значение ячейки if getattr(obj, field.name) == str(getattr(obj, field.name)): self.rec[key] = getattr(obj, field.name).encode('cp1251') else: self.rec[key] = getattr(obj, field.name) def end_object(self, obj): self.rec.store() def end_serialization(self): pass 

The code works, but there are no headers in the table, only data. Maybe someone knows a way to add a header, or where you can see detailed documentation on dbfpy. I also consider other modules if they have detailed documentation in English or Russian. Python 2.7, dbfpy 2.3.1, open the result using OpenOffice.

  • I can add that the list self.db.fieldNames (6 pcs.) Is correctly displayed in the console, and if you open the result.dbf with a notepad, then the data has a large array of NUL characters (226 characters). - Mae
  • Find on the Internet a description of the dbf format and make your export. The format is relatively simple. It's amazing how library writers can be wrong, everything has to be redone for them. If it works in Windows, you can connect dbf through odbc (only 32bit). Is there an odbc in python? - Sergey
  • Unfortunately, my knowledge of yesterday’s students is not enough to write my export module. And the author of dbfpy was not mistaken, his module was simply not documented, I had to dig deep into the code to solve the problem. - Mae
  • What is the difficulty there? Text file with title. - Sergey

1 answer 1

In all examples, after adding all records, it is recommended to close the database with the self.db.close () command.

In my case, this is not appropriate, because the file is saved in the self.stream I / O stream, with which the work continues in another module, and the close method closes this stream. Therefore, the end_serialization block was skipped. But in the dbfpy text, the close method is important, so we use the important part without closing the file. Now our end_serialization looks like this:

 def end_serialization(self): pass self.db.flush()