TypeError: 'in <string>' requires string as left operand, not bytes
dipu-bd opened this issue · comments
Error
Platform: I am running on Python 3.6 in Windows 8.1
Traceback (most recent call last):
File "dbf2csv.py", line 33, in <module>
execute(filename)
File "dbf2csv.py", line 19, in execute
for rec in in_db:
File "C:\ProgramData\Anaconda3\lib\site-packages\dbfpy3\dbf.py", line 243, in __getitem__
return self.RecordClass.fromStream(self, self._fixIndex(index))
File "C:\ProgramData\Anaconda3\lib\site-packages\dbfpy3\record.py", line 121, in fromStream
return cls.fromString(dbf, cls.rawFromStream(dbf, index), index)
File "C:\ProgramData\Anaconda3\lib\site-packages\dbfpy3\record.py", line 140, in fromString
[_fd.decodeFromRecord(string) for _fd in dbf.header.fields])
File "C:\ProgramData\Anaconda3\lib\site-packages\dbfpy3\record.py", line 140, in <listcomp>
[_fd.decodeFromRecord(string) for _fd in dbf.header.fields])
File "C:\ProgramData\Anaconda3\lib\site-packages\dbfpy3\fields.py", line 175, in decodeFromRecord
return self.decodeValue(self.rawFromRecord(record))
File "C:\ProgramData\Anaconda3\lib\site-packages\dbfpy3\fields.py", line 309, in decodeValue
if value in "NnFf ":
TypeError: 'in <string>' requires string as left operand, not bytes
Script
The following script works well for most DBF files. But gives the error above for some files. I did not understand why, but perhaps it is an issue with this repo?
#!/usr/bin/python
import csv
from dbfpy3 import dbf
import os
import sys
def execute(filename):
if filename.lower().endswith('.dbf'):
print("Converting %s to csv" % filename)
csv_fn = filename[:-4]+ ".csv"
with open(csv_fn,'w', newline='') as csvfile:
in_db = dbf.Dbf(filename)
out_csv = csv.writer(csvfile)
names = []
for field in in_db.header.fields:
names.append(field.name)
out_csv.writerow(names)
for rec in in_db:
out_csv.writerow(rec.fieldData)
in_db.close()
print("Done...")
else:
print("Filename does not end with .dbf")
folder = sys.argv[1]
if os.path.isfile(folder):
execute(folder);
else:
for name in os.listdir(folder):
filename = os.path.join(folder, name)
execute(filename)
The issue was fixed after adding the following line at line 307 in fileds.py
value = value.decode('utf-8')