• python(43) dbf文件读写


    自己封装的dbf文件操作库

    dbf库能完成增删改查操作且一直有更新所以使用此库进行封装

    1. import dbf
    2. class DbfHelper:
    3. def __init__(self, path, field_specs=None):
    4. """
    5. :param path:
    6. :param field_specs: 'name C(30); age N(3,0); birth D'
    7. field_specs 有内容时为新建(会覆盖已有文件),无内容时不会新建
    8. """
    9. self.table = dbf.Table(path, field_specs)
    10. self.table.open(mode=dbf.READ_WRITE)
    11. def append_data(self, data):
    12. """
    13. :param data: tuple or list : (('jjj',111,dbf.Date(2000,1,2),(),())
    14. :return:
    15. """
    16. for item in data:
    17. self.table.append(item)
    18. def add_fields(self,field_specs):
    19. """
    20. :param field_specs: 'name C(30); age N(3,0); birth D'
    21. :return:
    22. """
    23. self.table.add_fields(field_specs)
    24. def set_field_value(self, row, field, value):
    25. with self.table[row] as rd:
    26. setattr(rd, field, value)
    27. def set_value_by_index(self, row, col, value):
    28. with self.table[row] as rd:
    29. rd[col] = value
    30. def close(self):
    31. self.table.close()
    32. def get_field_names(self):
    33. return list(self.table.field_names)
    34. def convert_list_dict(self):
    35. field_nams = list(self.table.field_names)
    36. return [ dict(zip(field_nams,list(record))) for record in dd.table]
    37. def delete_fields(self,fields):
    38. """
    39. 删除对应列及数据
    40. :param fields:
    41. :return:
    42. """
    43. self.table.delete_fields(fields)
    44. def delete(self,row):
    45. dbf.delete(self.table[row])
    46. self.table.pack()
    47. if __name__ == '__main__':
    48. dd = DbfHelper('temptable11.dbf')
    49. data = [('John Doe', 31, dbf.Date(1979, 9,13)),('Ethan Furman', 102, dbf.Date(1909, 4, 1)),('Jane Smith', 57, dbf.Date(1954, 7, 2)),('John Adams', 44, dbf.Date(1967, 1, 9)),]
    50. # print(dd.table[2])
    51. # print(type(dd.table[2]))
    52. # print(dd.table[2].name)
    53. # dd.table[2].name = 'sdfsgdgb'
    54. # print(dd.table[2])
    55. # #dd.append_data(data)
    56. # #dd.append_data(data)
    57. # #dd.close()
    58. # #dd.add_fields('tt1 C(30); tt2 N(3,0)')
    59. # dd.close()
    60. for record in dd.table:
    61. print(record)
    62. # for record in dd.table:
    63. # with record as red:
    64. # red[0] = '12133111'
    65. #
    66. # with dd.table[0] as rd:
    67. # rd[0] = 'skjdhfgbshdfhhs'
    68. #
    69. # print(dd.table[0])
    70. # print(getattr(dd.table[0], 'name'))
    71. # with dd.table[0] as rd:
    72. # setattr(rd, 'name', 'eee')
    73. # print(dd.table[0])
    74. # for record in dd.table:
    75. # print(record)
    76. #print(dd.convert_list_dict())
    77. #dd.table.delete_fields('TT1')
    78. #dd.table.pack()
    79. dbf.delete(dd.table[0])
    80. print("================================")
    81. dd.table.pack()
    82. for record in dd.table:
    83. print(record)

    参考:

    官方:dbf · PyPI

    Python Write value in dbf file - Stack Overflow

  • 相关阅读:
    Django 基础教程
    MySQL数据库之索引
    浅议.NET遗留应用改造
    10/16作业
    Springboot+网上投资借贷中介服务 毕业设计-附源码221506
    基于模型改进的LADRC在废液焚烧系统中的应用
    RIS 系列 TransVG++: End-to-End Visual Grounding with Language Conditioned Vision Transformer 论文阅读笔记
    C++并发编程实战 第二版 第二章
    Python中json的用法
    常见的设计模式
  • 原文地址:https://blog.csdn.net/qq_37674086/article/details/125885945