我完全不熟悉編程。這是我的第一個「適當」項目,將被其他人使用。在文件中的某個點追加,每次增加一個值
該程序提出了各種問題,然後寫入一個新的商店入口文件。對於一個空文件,我大部分都可以工作,但成品需要將條目插入現有文件中的特定點。
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