I am trying to convert a specific DBF file into a CSV format, but my script can't convert a specific value. I'm using the ydbf package in Python.
In total I have 598 DBF files to be converted, where I standardly used this code:
with ydbf.open(filename, encoding=encoding) as dbf:
for record in dbf:
records[index] = record
index += 1
df = pd.DataFrame.from_dict(records, orient='index')
df.to_csv(filename[:-4] + ".csv", index=False)
For 6 files there are some errors remaining, 5 out of those six files were solved using the code below, but the last file keeps returning the value error. I tried several encoding methods: latin, cp1250, cp1251, cp1252 and ascii, but they all return the same error:
Error occured (ValueError: invalid literal for int() with base 10: b'RE') while reading rec #0.
I also tried:
dbf = DBF(file)
dataResult = pd.DataFrame(iter(dbf))
But this returns the same error.
A sample of my dbf file:
* Á ' ALUPROF C XB N
YB N LAAG N VOLGNR N!
EK_BDA20-114 3.0000 20.0000RE 100 EK_BDA20-114 1.5000 19.5981RE 200 EK_BDA20-114 0.4019 18.5000RE 300 EK_BDA20-114 0.0000 17.0000RE 400 EK_BDA20-114 0.0000 0.0000RE 500 EK_BDA20-114 114.0000 0.0000RE 600 EK_BDA20-114 114.0000 17.0000RE 700 EK_BDA20-114 113.5981 18.5000RE 800 EK_BDA20-114 112.5000 19.5981RE
try:
df = pd.DataFrame()
for file in dbf3_file_errors['filename']:
with ydbf.open(file, encoding="ascii") as dbf:
for record in dbf:
updated_record = []
for value in record:
try:
float_value = float(value)
updated_record.append(float_value)
except ValueError:
try:
int_value = int(value)
updated_record.append(int_value)
except ValueError:
try:
str_value = str(value)
updated_record.append(str_value)
except:
if value == 'RE':
updated_record.append(np.nan)
df = df.append(pd.DataFrame([updated_record]), ignore_index=True)
break
df.to_csv(file[:-4] + ".csv", index=False)
except Exception as e:
print("Error occured:", e)