我有一個小數據庫,文本文件db.txt:Python。如何編輯文本數據庫中的數據?
(peter)
name = peter
surname = asd
year = 23
(tom)
name = tom
surname = zaq
year = 22
hobby = sport
(paul)
name = paul
surname = zxc
hobby = music
job = teacher
如何獲得的所有數據部分來自例如湯姆?我想在變量獲得:
(tom)
name = tom
surname = zaq
year = 22
hobby = sport
然後我要改變數據:
replace("year = 22", "year = 23")
,並得到:
(tom)
name = tom
surname = zaq
year = 23
hobby = sport
現在添加(工作)和刪除(姓)數據:
(tom)
name = tom
year = 23
hobby = sport
job = taxi driver
最後重寫改變部分到舊的db.txt文件:
(peter)
name = peter
surname = asd
year = 23
(tom)
name = tom
year = 23
hobby = sport
job = taxi driver
(paul)
name = paul
surname = zxc
hobby = music
job = teacher
任何解決方案或提示如何做到這一點?非常感謝!
我打算做很多像」add job「或」change age「這樣的查詢,你應該考慮將你的數據遷移到SQLITE da tabase,這是比非標準文本文件更適合存儲和操作數據的解決方案。 –
您的問題更像是「我應該使用哪種結構」還是像「如果信息發生變化,我真的必須每次讀寫整個文件?」 –