Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: 'in <string>' requires string as left operand, not bytes #4

Open
dipu-bd opened this issue May 28, 2017 · 1 comment
Open

Comments

@dipu-bd
Copy link

dipu-bd commented May 28, 2017

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)
@dipu-bd
Copy link
Author

dipu-bd commented May 28, 2017

The issue was fixed after adding the following line at line 307 in fileds.py

value = value.decode('utf-8')

ZanSara added a commit to ZanSara/dbfpy3 that referenced this issue Jan 3, 2019
ZanSara added a commit to ZanSara/dbfpy3 that referenced this issue Jan 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant