我完全不熟悉编程。这是我的第一个“适当”项目,将被其他人使用。在文件中的某个点追加,每次增加一个值
该程序提出了各种问题,然后写入一个新的商店入口文件。对于一个空文件,我大部分都可以工作,但成品需要将条目插入现有文件中的特定点。
2问题已经我百思不得其解:
如何插入新开店项目到文件之前,“返回:”我如何增加“InventoryLocation:”通过各1项被添加
时间该文件被附加到具有这种结构:
# shop 1
SuperSpaceSquids:
RewardType: playercommand
PriceType: free
Reward:
- ewarp Shop-SuperSpaceSquids
MenuItem:
- type:SKULL_ITEM
- playerhead:MHF_Squid
- durability:3
- amount:1
- name:&5SuperSpaceSquids
- 'lore:&6&o"Squid Shop From Space!"'
Message: ''
InventoryLocation: 38
ExtraPermission: ''
# shop 2
HadesKitty:
RewardType: playercommand
PriceType: free
Reward:
- ewarp Shop-HadesKitty
MenuItem:
- type:SKULL_ITEM
- playerhead:Turtle_Em
- durability:3
- amount:1
- name:&5HadesKitty
- 'lore:&6&o"our prices are fair!..."'
Message: ''
InventoryLocation: 39 # This value needs to be incremented by 1 each time
ExtraPermission: ''
>> insert new shops here <<
Back:
RewardType: shop
PriceType: free
Reward: Shop_Menu
MenuItem:
- type:REDSTONE
- amount:1
- name:&cBack
- lore:&8Back to Shop Menu
InventoryLocation: 54
这是写入到文件中的功用:
def write(shop, id, data, desc, skull):
f = open('file.yml', 'w')
f.write(" %s:" % shop)
f.write("\n RewardType: playercommand")
f.write("\n PriceType: free")
f.write("\n Reward:")
f.write("\n - ewarp shop-%s" % shop)
f.write("\n MenuItem:")
if skull:
f.write("\n - Type:SKULL_ITEM")
f.write("\n - playerhead:%s" % skull)
f.write("\n - durability:3")
if not skull:
f.write("\n - id:%s" % id)
if data:
f.write("\n - durability:%s" % data)
f.write("\n - amount:1")
f.write("\n - name:&5%s" % shop)
f.write("\n - 'lore:&6&o\"%s\"'" % desc)
f.write("\n Message:")
f.write("\n InventoryLocation:")
f.write("\n ExtraPermission: ''")
f.flush()
print "\nAll done."
print "\nHit Return to quit or 1 to add more shops."
while True:
choice = raw_input(prompt)
if choice == "":
print "\nGoodbye!"
f.close()
time.sleep(2)
exit(0)
elif choice == "1":
os.system('cls' if os.name == 'nt' else 'clear')
input()
else:
print "I dont understand that."
我想指出你的设计存在一些严重的问题,这些问题太广泛而无法在本网站上得到解答。你应该研究数据库理论,并习惯使用几个不同的数据库(我建议SQLite启动,后来Postgres)。实施它你自己*会*搞砸了。 – o11c