如何在不删除任何旧数据的情况下向.txt文件添加新信息?

2024-09-17 18:28:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试创建一个kivy python应用程序,它涉及到将用户输入存储到.txt文件,但每次我将用户输入添加到.txt文件时,它总是删除我的旧信息。我如何才能阻止这种情况发生,或者是否有另一种简单且无成本的替代方案


1条回答
网友
1楼 · 发布于 2024-09-17 18:28:35

您可以使用Json文件。开发人员建议使用JsonStore,保存到txt不知何故不是很好。此外,如果您想将应用程序传输到移动设备,它将更易于使用

from kivy.storage.jsonstore import JsonStore

store = JsonStore('file name.json')

# put some values
store.put('tito', name='Mathieu', org='kivy')
store.put('tshirtman', name='Gabriel', age=27)

# using the same index key erases all previously added key-value pairs
store.put('tito', name='Mathieu', age=30)

# get a value using a index key and key
print('tito is', store.get('tito')['age'])

# or guess the key/entry for a part of the key
for item in store.find(name='Gabriel'):
    print('tshirtmans index key is', item[0])
    print('his key value pairs are', str(item[1]))

More information

相关问题 更多 >