First you should know what version of Dbf you have, só read the first byte of the file:
path = "/path/to/dbf/file.dbf"
with open(path, "rb") as f:
byte = f.read(1)
print(f"You have a DBF {int.from_bytes(byte)} file.")
Example:
> You have a DBF 3 file.
If you have a Dbf 5 file, everything will be fine, but if, which is my case most of the times, you have a Dbf 3 file, you have to tweek @andy-hayden solution by using simpledbf:
Following this issue, basically you should create a class Dbf3 that inherits Dbf5, but you need to add an new condition to the _get_recs method.
import struct
from simpledbf import Dbf5
class Dbf3(Dbf5):
def __init__(self, dbf, codec='utf-8'):
super().__init__(dbf, codec)
def _get_recs(self, chunk=None):
#[...copy the code from the original class up until line 664...]
elif typ == 'M':
value = self._na
#[...copy the code from the original class after 664...]
Original Dbf code for reference
Then your new class Dbf3 will be able to read and convert Dbf3 files with ease:
dbf = Dbf3(filename, codec="iso-8859-1") #codec specific to this dataset
dbf.to_csv("converted_dbf.csv")