書き込むデータの種類を読み込んで保存するパーサーがありますか? ファイル形式は、読み取り可能な形式でなければなりません。 棚には表示されません。Python iniパーサー
答えて
iniファイル形式の設定ファイルを読むためにConfigParser
クラスを使用します。
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なので、格納された値の形式が読める人間です。
あなたはconfigobj
ライブラリーでは、次の機能
def getvalue(parser, section, option):
try:
return parser.getint(section, option)
except ValueError:
pass
try:
return parser.getfloat(section, option)
except ValueError:
pass
try:
return parser.getbool(section, option)
except ValueError:
pass
return parser.get(section, option)
ブール値(1または0としてiniファイルに格納されています)を格納すると、intバックを取得します。 –
これは重要な制限です。はい。残念なことに、正確な意味を知ることは不可能です。 (できるだけ多くの値がある場合のように)。幸運にも、もし 'getvalue(パーサ、" asection "、" boollookslikeint ")のようなものを使用すれば問題はありません: – Robin
最終的に、OPは単にiniファイル形式のものを保存するだけです。データ型を戻すことができます。 –
を使用することができ、それは非常に簡単になります。
import sys
import json
from configobj import ConfigObj
if(len(sys.argv) < 2):
print "USAGE: pass ini file as argument"
sys.exit(-1)
config = sys.argv[1]
config = ConfigObj(config)
今、あなたは、目的の設定を抽出するために辞書としてconfig
を使用することができます。
json
に変換したい場合は、それも簡単です。
config_json = json.dumps(config)
print config_json
- 1. Python SAXパーサー
- 2. Python CSSパーサー
- 3. Python 3.1 RSSパーサー?
- 4. Python SVGパーサー
- 5. Python tnsnames.oraパーサー
- 6. AMPLデータファイルのPythonパーサー
- 7. 非従来のINIをPythonで読む
- 8. Pythonのminidom XMLパーサー3
- 9. Python PLYパーサー構文エラー
- 10. Pythonのパーサーの複雑さ
- 11. Pythonパーサーの出力なし
- 12. pythonのChronic(Ruby NLP日付/時刻パーサー)?
- 13. python grub.cfgパーサーはありますか?
- 14. Python、Tweepy、configパーサーに関する詳細
- 15. PythonのRegExを使った関数パーサー
- 16. Java/C/C++/Python/shellのコマンドラインパラメータ用のパーサー
- 17. Bat To iniコンバータ
- 18. メモリINIファイルライタ
- 19. ハッキングゲームiniファイル
- 20. strpos:.iniファイル
- 21. マルチレベル辞書ini
- 22. OMNeT ++ .iniファイルパラメータ
- 23. Zend Config Iniキャッシング
- 24. PHP PDFパーサーSMALOtとTCPDFパーサー
- 25. JQueryテーブル・パーサーのカスタム・パーサー
- 26. ここ.BATとの.ini
- 27. プロパティファイル(iniファイル)はPythonパッケージに含まれていません
- 28. INIファイルのセクションのパラメータ値を変更するPython
- 29. 削除コメントiniファイル
- 30. PowerShellでINI編集
[のConfigParser](http://docs.python.org/library/configparser.html) –
んConfigParserのは、あなたがやりたいことではありませんか? –
私は自動タイプの検出をサポートする必要があります。 ConfigParserはすべてを文字列として読み込みます。 – Evgeny