so I am using python's dbf module (https://pypi.org/project/dbf/), and I want to update a key, value pair in a table. I have the following keys and values:
>>> for record in dbf.Process(table):
... print record
...
0 - store : 590
1 - date : u'12/17/19'
2 - check_num : 4
3 - seq_main : 1
4 - option : u'FALSE'
5 - item_num : 263
I want to update the 'date' field to datetime.date(2019, 12, 17). However, when I do the following:
>>> for record in dbf.Process(table):
... record.date = datetime.date(2019, 12, 17)
...
I get the error:
File "/Users/me/Library/Python/2.7/lib/python/site-packages/dbf/__init__.py", line 3946, in update_character
raise ValueError("unable to coerce %r(%r) to string" % (type(string), string))
ValueError: unable to coerce <type 'datetime.date'>(datetime.date(2019, 12, 17)) to string
Does anyone have an idea on how to convert the string date to a datetime object? Any help is appreciated.
The field is actually a character field -- it just happens to store date information in it. You'll have to convert your date into text yourself and then save it:
>>> for record in dbf.Process(table):
... record.date = datetime.date(2019, 12, 17).strftime('%m/%d/%y')
...
If your date
field is 10 characters or more, change the %y
to %Y
to get a full 4-digit year.