简体   繁体   English

覆盖文件python中的标志

[英]Overwrite flags in a file python

I have a file in the following format: 我有以下格式的文件:

[character1]    
health = 100    
lives = 3        
some other flags

[character2]    
health = 50    
lives = 1    
etc

[character3]    
missing lives line    
some other flags

I have information on the updated lives in such a format: lives[char][status] where for character1 it would look like lives['character1']['lives = 3'] 我以以下格式获取有关更新的生命的信息: lives[char][status]对于character1它看起来像lives['character1']['lives = 3']

So what I am trying to do is go through the file and updating the lives based on the information above, and add missing lives flags such as character3 所以我想做的就是浏览文件并根据上述信息更新生活,并添加诸如character3缺少生活标志

with open('characters.txt', 'rw') as chars:
    for line in chars:
        if line.find('[') is not None:
            character = line
        if line.find('lives =') is not None:
            if line != lives[character][status]
                line = line.replace(lives[character][status])
            chars.write(line)

That's my general logic behind it but it looks like character is being set to the line following it instead ( health = 100 ) 这是我背后的一般逻辑,但是看起来字符已设置在其后的行中( health = 100

any help would be greatly appreciated! 任何帮助将不胜感激!

I strongly suggest that you store your character data in a dictionary and export/import them as JSON. 我强烈建议您将字符数据存储在字典中,并将其作为JSON导出/导入。 This will save you a lot of headaches. 这将为您节省很多麻烦。

For example, store your characters like this: 例如,像这样存储您的字符:

data = {'character1':{'lives':3, 'health':100}, 'character2':{'lives':4, 'health':85}}

You can write the content to a file like this: 您可以将内容写入文件,如下所示:

import json
with open('myfile', 'w') as f:
    f.write(json.dumps(data))

You can load your player data like this from a file: 您可以从文件中加载播放器数据,如下所示:

import json
with open('myfile', 'r') as f:
    data = json.load(f)

Now it is trivial to change the stats of a character. 现在,更改角色的统计信息变得微不足道了。 For example, character2's health drops to 50: 例如,character2的生命值降低至50:

data['character2']['health'] = 50

Or character1 dies: 或character1去世:

if data['character1']['health'] <= 0:        
    data['character1']['lives'] -= 1     

When you are done with the changes, write data back to the file with json.dumps . 完成更改后,请使用json.dumpsdata写回到文件中。

You should use the built-in ConfigParser module . 您应该使用内置的ConfigParser模块 It will deal with this directly: 它将直接处理此问题:

>>> i = '''[character1]
... health = 100
... lives = 3
...
... [character2]
... health = 50
... lives = 1
...
... [character3]
... lives = 2
... '''
>>> import ConfigParser
>>> import io
>>> config = ConfigParser.RawConfigParser(allow_no_value=True)
>>> config.readfp(io.BytesIO(i))
>>> config.get('character3', 'lives')
'2'

To read from a file, its even simpler: 要读取文件,它甚至更简单:

>>> config = ConfigParser.ConfigParser()
>>> config.readfp(open('some-file.txt'))
>>> config.get('character3', 'lives')

To make changes, and write out to a file: 进行更改并写出到文件中:

>>> config.set('character3', 'lives', '4')
>>> config.write(open('foo.txt','w'))
>>> config.readfp(open('foo.txt')) # Read the file again
>>> config.get('character3','lives') # Confirm the new value
'4'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM