[英]How to delete dbf rows containing an alpha value or an entire dbf column
我想刪除 dfb 上包含特定數據的一些行和列。
讓我們假設 dfb 的結構看起來像下面的圖像,並且我想刪除帶有“knapford”作為“Prov”和“Reg”列的行。
當我嘗試遵循此腳本時:
table = dbf.Table(wonderland.dbf).open(mode=dbf.READ_WRITE)
with table as tab:
print (table)
for record in dbf.Process(tab):
dbf.delete(record[0])
我收到此錯誤:
File "<ipython-input-4-d8feabfc9819>", line 4, in <module>
dbf.delete(record[0])
File "C:\Users\fassi\AppData\Local\Continuum\anaconda3\lib\site-packages\dbf\__init__.py", line 8349, in delete
if not template and record._meta.status == CLOSED:
AttributeError: 'int' object has no attribute '_meta'
我想到了另一個建議,但我想知道它是否真的會刪除行的所有字段,只是一些特定的字段或所有記錄!
table = dbf.Table(wonderland.dbf).open(mode=dbf.READ_WRITE)
with table as tab:
dbf.delete_fields(table, Reg)
for record in dbf.Process(tab):
if record[0].strip()=='Knapford': #since we've delete the Reg, Prov is becoming record[0]?
dbf.delete(record)
提前致謝
我認為這應該有效:
from simpledbf import Dbf5
dbf = Dbf5('file.dbf')
data = dbf.to_dataframe()
for row in range(len(data)):
Reg= data.loc[row,'Reg']
Prov = data.loc[row,'Prov']
if Reg =='knapford' or Prov =='knapford':
data.drop(index=row, axis=0, inplace=True)
這個其他腳本工作得很好:
import dbf
with table as tab:
for record in dbf.Process(tab):
if record[1].strip()=='Knapford':
#print (record)
dbf.delete(record)
table.pack()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.