是否有一个解析器可以读取和存储要写入的数据类型? 文件格式必须产生可读性。 搁置不提供。
请您参考如下方法:
使用ConfigParser类读取ini文件格式的配置文件:
http://docs.python.org/library/configparser.html#examples
ini 文件格式不存储所存储值的数据类型(您需要在读回数据时知道它们)。您可以通过以 json 格式编码您的值来克服此限制:
import simplejson
from ConfigParser import ConfigParser
parser = ConfigParser()
parser.read('example.cfg')
value = 123
#or value = True
#or value = 'Test'
#Write any data to 'Section1->Foo' in the file:
parser.set('Section1', 'foo', simplejson.dumps(value))
#Now you can close the parser and start again...
#Retrieve the value from the file:
out_value = simplejson.loads(parser.get('Section1', 'foo'))
#It will match the input in both datatype and value:
value === out_value
作为 json,存储值的格式是人类可读的。
