Python dbf:如何更新值?
Python dbf: How to update a value?
所以我正在使用 python 的 dbf 模块 (https://pypi.org/project/dbf/),我想更新 table 中的键值对。我有以下键和值:
>>> 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
我想将 'date' 字段更新为 datetime.date(2019, 12, 17)。但是,当我执行以下操作时:
>>> for record in dbf.Process(table):
... record.date = datetime.date(2019, 12, 17)
...
我收到错误:
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
有没有人知道如何将字符串日期转换为日期时间对象?感谢任何帮助。
该字段实际上是一个字符字段——它只是恰好在其中存储了日期信息。您必须自己将日期转换为文本,然后保存:
>>> for record in dbf.Process(table):
... record.date = datetime.date(2019, 12, 17).strftime('%m/%d/%y')
...
如果您的 date
字段为 10 个或更多字符,请将 %y
更改为 %Y
以获得完整的 4 位数年份。
所以我正在使用 python 的 dbf 模块 (https://pypi.org/project/dbf/),我想更新 table 中的键值对。我有以下键和值:
>>> 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
我想将 'date' 字段更新为 datetime.date(2019, 12, 17)。但是,当我执行以下操作时:
>>> for record in dbf.Process(table):
... record.date = datetime.date(2019, 12, 17)
...
我收到错误:
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
有没有人知道如何将字符串日期转换为日期时间对象?感谢任何帮助。
该字段实际上是一个字符字段——它只是恰好在其中存储了日期信息。您必须自己将日期转换为文本,然后保存:
>>> for record in dbf.Process(table):
... record.date = datetime.date(2019, 12, 17).strftime('%m/%d/%y')
...
如果您的 date
字段为 10 个或更多字符,请将 %y
更改为 %Y
以获得完整的 4 位数年份。